jquery按属性名称选择每个值结束文本

jquery按属性名称选择每个值结束文本,jquery,jquery-selectors,attributes,each,prepend,Jquery,Jquery Selectors,Attributes,Each,Prepend,按特定属性名称选择元素,在本例中为图标: <a href="#" icon="one.png">link</a> <a href="#" icon="two.png">link</a> <a href="#" icon="three.png">link</a> 然后,在每个元素前面加上元素属性值的文本, 例如: 如何操作?尝试使用。在使用接收器功能完成任务之前 $('a[icon]').before(function(){

按特定属性名称选择元素,在本例中为图标:

<a href="#" icon="one.png">link</a>
<a href="#" icon="two.png">link</a>
<a href="#" icon="three.png">link</a>
然后,在每个元素前面加上元素属性值的文本, 例如:

如何操作?

尝试使用。在使用接收器功能完成任务之前

$('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(){
   $(this).before('<span>'+$(this).attr('icon')+'</span>') 
});
试试这个:

$('a[icon]').each(function(){
   $('<span />', { text : this.getAttribute('icon') }).insertBefore(this);
});

你试过什么了???@downvoter想评论一下,这是一个新手犯的错误。必须使用的方法是。insertBefore。更新。
$('a[icon]').each(function(){
   $('<span />', { text : this.getAttribute('icon') }).insertBefore(this);
});