我想在所有按钮元素之后插入一个span标签。
我现在拥有的是:
<button>button text 1</button>
<button>button text 2</button>
<button>button text 3</button>我想要的是:
<button><span>button text 1</span><button>
<button><span>button text 2</span><button>
<button><span>button text 3</span><button>我试过用
var content = $('button').html();
$('button').empty().html('<span>' + content + '</span>');但是,如果div上有多个按钮,它就会将第一个值复制到其余的按钮上。
发布于 2018-03-13 12:04:56
$('button').each(function(){
var content = $(this).html();
$(this).empty().html('<span>' + content + '</span>');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Hello!</button>
<button>Hello!</button>
<button>Hello!</button>
使用$.each jQuery函数:
$('button').each(function(){
var content = $(this).html();
$(this).empty().html('<span>' + content + '</span>');
});文档:http://api.jquery.com/jquery.each/
https://stackoverflow.com/questions/49255638
复制相似问题