简体   繁体   English

JavaScript返回[object Object]

[英]JavaScript returns [object Object]

I'm trying to get this running 我正试图让这个运行起来

$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').contents()+'<a class="close-reveal-modal">&#215;</a></div>');

but It just gives me [object Object]. 但它只是给了我[object Object]。 As I only replace it with $('#info').contents() things work fine. 因为我只用$('#info')替换它.content()一切正常。

Try: 尝试:

$(function(){
    var content = $('#info').get(0).outerHTML;
    $('#info').replaceWith('<div id="infobox" class="reveal-modal">'+content+'<a class="close-reveal-modal">&#215;</a></div>');
})

The $('#info').get(0).outerHTML returns <div id="info">anything</div> . $('#info').get(0).outerHTML返回<div id="info">anything</div> But if just want the content, use $('#info').html() , that returns anything . 但是如果只想要内容,请使用$('#info').html() ,它返回anything

If you're trying to maintain the existing content and place it inside an outer div try using wrap() and you can call after() to adding the closing anchor. 如果您正在尝试维护现有内容并将其置于外部div中,请尝试使用wrap()然后调用after()以添加结束锚点。

$('#info').wrap('<div id="infobox" class="reveal-modal">')
    .after('<a class="close-reveal-modal">&#215;</a>')

Working example: http://jsfiddle.net/hunter/qU4s3/ 工作示例: http//jsfiddle.net/hunter/qU4s3/


Could also be written like this: 也可以这样写:

$('#info')
    .wrap($('<div>', { id: "infobox", "class": "reveal-modal"}))
    .after($('<a>', { "class": "close-reveal-modal", html: "&#215;" }));

Working example: http://jsfiddle.net/hunter/qU4s3/2/ 工作示例: http//jsfiddle.net/hunter/qU4s3/2/

.contents()返回一个jQuery对象,你想要的是.text().html()这取决于#info包含其他元素。

$('#info').replaceWith('<div id="infobox" class="reveal-modal">'+$('#info').text()+'<a class="close-reveal-modal">&#215;</a></div>');

starts with $('#info').contents(). $('#info').contents().开头$('#info').contents(). <-- like this < - 像这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM