我正在尝试让这个运行起来
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').contents()+'<a class="close-reveal-modal">×</a></div>');
但是它只给了我[object Object]。当我把它替换为$('#info').contents()时,事情就正常运行了。
我正在尝试让这个运行起来
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').contents()+'<a class="close-reveal-modal">×</a></div>');
尝试:
$(function(){
var content = $('#info').get(0).outerHTML;
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+content+'<a class="close-reveal-modal">×</a></div>');
})
$('#info').get(0).outerHTML
返回的是<div id="info">anything</div>
。但如果只想要内容,请使用$('#info').html()
,它将返回anything
。
wrap()
,然后可以调用 after()
来添加关闭锚点。$('#info').wrap('<div id="infobox" class="reveal-modal">')
.after('<a class="close-reveal-modal">×</a>')
工作示例:http://jsfiddle.net/hunter/qU4s3/
也可以这样写:
$('#info')
.wrap($('<div>', { id: "infobox", "class": "reveal-modal"}))
.after($('<a>', { "class": "close-reveal-modal", html: "×" }));
.append
应该改为 .after
,并且 wrap
应该包含它。 - Fabrício Matté.contents()
会返回一个jQuery对象,您需要使用.text()
或 .html()
,具体取决于#info
中是否包含其他元素。
$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').text()+'<a class="close-reveal-modal">×</a></div>');
以 $('#info').contents().
开头 <-- 就像这样
$('#info').contents()
是一个对象,对象的默认字符串表示形式是[object Object]
。你不能仅仅将任意对象与字符串连接起来。 - Felix Kling