[英]jquery select by attribute name each this value pepend text
通过某个属性名称选择元素,在这种情况下为“ icon”:
<a href="#" icon="one.png">link</a>
<a href="#" icon="two.png">link</a>
<a href="#" icon="three.png">link</a>
然后对每个元素,在<span>
添加元素属性值的文本,
例:
<span>one.png</span><a href="#" icon="one.png">link</a>
<span>two.png</span><a href="#" icon="two.png">link</a>
<span>three.png</span><a href="#" icon="three.png">link</a>
怎么做?
尝试将.before()
与接收函数一起使用以完成任务,
$('a[icon]').before(function(){
return $("<span>", {text: $(this).attr('icon')});
})
尝试这个 :
$('a[icon]').each(function(){
$(this).before('<span>'+$(this).attr('icon')+'</span>');
});
尝试这个:
$('a[icon]').each(function(){
$('<span />', { text : this.getAttribute('icon') }).insertBefore(this);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.