我在这里完成插入元素任务时遇到了麻烦,我已经为此花费了数小时。
当用户单击按钮时,我尝试按类名插入元素。我在脚本中也有 css 设置。我的问题是只有第一个元素具有 css 属性,而其余元素没有。任何人都可以帮助我吗?非常感谢!
jQuery
//when users clicks the button
var imgForClass = $("<img src='images/anim.gif' class='imgAbs'>");
var cssProp = {'position':'absolute',
'z-index':99,
'top': topPos, //topPos is the position that the element I want to attach to.
'left': leftPos //leftPos is the position that the element I want to attach to.
}
//detect if the element has id of 'cons'
if($element.is('#cons'){
imgForClass.insertBefore('.conElement');
imgForClass.css(cssProp)
}
html
<div>
<img src='a.jpg'/>
<img id='cons' src='b.jpg'/>
</div>
//the new element will be inserted here but only the first element has css property(z-index...etc)
<div class='conElement'>
<img src='b.jpg'/>
</div>
//the new element will be inserted here but has no css property(z-index...etc)
<div class='conElement'>
<img src='b.jpg'/>
</div>
//the new element will be inserted here but has no css property(z-index...etc)
<div class='conElement'>
<img src='b.jpg'/>
</div>
//the new element will be inserted here but has no css property(z-index...etc)
<div class='conElement'>
<img src='b.jpg'/>
</div>
...more <div>