javascript - 具有相同 id 的 jQuery .before

标签 javascript jquery html

我只是想问一下jQueryjQuery 可以做到这一点:

<h1 id="lorem-ipsum">Hello World!</h1>

对此:

<div id="lorem-ipsum"></div>
<h1>Hello World!</h1>

最佳答案

你的意思是像这个人:工作演示位动态版本​​:http://jsfiddle.net/QSdqM/ http://jsfiddle.net/GQzFU/

希望它能帮助解决问题:)

API:http://api.jquery.com/replaceWith/

代码

$('h1').replaceWith(function() {

   return '<div id="' + this.id + '"></div><h1>' + $(this).text() + '</h1>'

});

$('h1').replaceWith('<div id="lorem-ipsum"></div><h1>Hello World!</h1>');


alert($('body').html());​

关于javascript - 具有相同 id 的 jQuery .before,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11606491/

相关文章:

javascript - 如何居中一个宽的div

Javascript - onclick 事件不起作用

jquery - Twitter Bootstrap 2.3.2 'affix' 无法在移动设备上运行

html - 表 <colgroup> 忽略最小-最大宽度

javascript - 使用 getUserMedia() 读取二维码

javascript - 未设置子组件中输入属性的 Angular 异步管道

javascript - 使用 href 将文本字段值发送到 php

php - jQuery 验证插件 : How to create your own messages

JavaScript 变量带有我需要在 Html.ActionLink 中使用的 ID

html - 如何在utf8中进行颠倒的感叹号