Javascript 使用.replaceWith更改dom

Javascript 使用.replaceWith更改dom,javascript,jquery,replacewith,Javascript,Jquery,Replacewith,我有一些代码几乎完全符合我的要求。它将所有强标记更改为样式化的h3标记,这是完美的,但就我而言,我不知道该替换什么。单击以使其自动运行。我试过了。准备好了,它在我的jquery中给了我一个错误 $(document).ready(function(){ $('strong').click(function(){ $(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.inne

我有一些代码几乎完全符合我的要求。它将所有强标记更改为样式化的h3标记,这是完美的,但就我而言,我不知道该替换什么。单击以使其自动运行。我试过了。准备好了,它在我的jquery中给了我一个错误

$(document).ready(function(){
    $('strong').click(function(){
        $(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'))
    })
});
您希望使用,以便它对所有这些对象进行迭代

$('strong').each(function(){ ... });

您要做的是向页面上的所有元素添加一个处理程序,只要单击其中一个元素,就会触发该元素。如果要在文档准备就绪后立即执行替换,请尝试以下操作:

$(document).on('ready', function () {
    $('strong').each(function () {
        $(this).replaceWith($('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>'));
    });
});
这应该可以做到

$document.readyfunction{ $'strong'。每个函数{ $this.replace为$+this.innerHTML+ } }; 1. 2.
3我建议直接去替换:

$'strong'.replaceWithfunction{ 返回$+this.innerHTML+; }; 1. 2. 3.
4您会遇到什么错误?不需要。每个,因为replaceWith允许您给出一个函数。
// most (if not all) jQuery methods iterate over
// the collection to which they're chained:
$('strong').replaceWith(function () {

  // here 'this' is the individual <strong> element over
  // which the method iterates, and we return the created element:
  return $('<h3 style="margin:0px;display:inline;">' + this.innerHTML + '</h3>');
});