Javascript jquery更改标记
我的代码不起作用,你能帮我吗?我想将class=“s7”的标记名“p”更改为“h1”Javascript jquery更改标记,javascript,jquery,tags,Javascript,Jquery,Tags,我的代码不起作用,你能帮我吗?我想将class=“s7”的标记名“p”更改为“h1” $(文档).ready(函数(){ $(“.s7”).replace为($(''+$(this.html()+'')); }); 您缺少一个右括号,并且在错误的上下文中使用了它: $(document).ready(function(){ $(".s7").replaceWith($('<h1>' + $(".s7").html() + '</h1>')); }); “rep
$(文档).ready(函数(){
$(“.s7”).replace为($(''+$(this.html()+''));
});
您缺少一个右括号,并且在错误的上下文中使用了它:
$(document).ready(function(){
$(".s7").replaceWith($('<h1>' + $(".s7").html() + '</h1>'));
});
“replaceWith()”调用中的
this
的值不是“s7”元素;而是更大的“document.ready”处理程序中的this
的值
要执行所需操作,请使用“.each()”:
问题是,您正在将所有元素与类
s7
匹配,但您需要逐个处理它们,以便将其内容复制到新元素中。在当前代码中,此
始终是文档
,而不是当前元素
您可以使用来迭代匹配的元素:
$(".s7").each(function() {
var $this = $(this);
$this.replaceWith($("<h1>" + $this.html() + "</h1>"));
});
$(“.s7”)。每个(函数(){
var$this=$(this);
$this.replaceWith($(“+$this.html()+”);
});
或者可能:
$(".s7").each(function() {
$("<h1>" + $(this).html() + "</h1>").replaceAll(this);
});
$(“.s7”)。每个(函数(){
$(“”+$(this.html()+).replaceAll(this);
});
在您的示例中发生了什么?这不就是您的文档吗?您在最后缺少了一个)
。哦,是的,我刚才看到了您的答案,正在检查我的:-)谢谢!
$('.s7').each(function() {
$(this).replaceWith($('<h1>' + $(this).html() + '</h1>'));
});
$('.s').each(function(index, element) {
$(element).replaceWith($('<h1>' + $(element).html() + '</h1>'));
});
$(".s7").each(function() {
var $this = $(this);
$this.replaceWith($("<h1>" + $this.html() + "</h1>"));
});
$(".s7").each(function() {
$("<h1>" + $(this).html() + "</h1>").replaceAll(this);
});