Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否将jQuery对象和内容转换为字符串?_Jquery_Stringification - Fatal编程技术网

是否将jQuery对象和内容转换为字符串?

是否将jQuery对象和内容转换为字符串?,jquery,stringification,Jquery,Stringification,我正在抓取各种jquery对象,将它们放入一个数组中,然后作为HTML输出 我正在尝试将jQuery对象转换为文本字符串,以便以后可以将其作为HTML输出 我目前正在使用这种技术: console.log($myObject.clone().wrap('<div></div>').html()); console.log($myObject.clone().wrap(''.html()); 然而,这似乎只是抓住了我的对象的内容 例如,如果$myObject是My Ti

我正在抓取各种jquery对象,将它们放入一个数组中,然后作为HTML输出

我正在尝试将jQuery对象转换为文本字符串,以便以后可以将其作为HTML输出

我目前正在使用这种技术:

console.log($myObject.clone().wrap('<div></div>').html());
console.log($myObject.clone().wrap(''.html());
然而,这似乎只是抓住了我的对象的内容

例如,如果$myObject是
My Title
,则上面只返回“My Title”(无H2标记)

我也尝试使用.text(),但得到了相同的结果


有没有办法将整个jQuery对象转换为文本字符串?

您要找的不是将jQuery对象转换为字符串,而是将DOM节点转换为其HTML表示形式

JQuery中没有内置的方法可以做到这一点,Javascript中也没有类似的方法

这有点乏味,但是你可以重建它。 下面的代码既不完整也没有经过测试,但这里是想法

var h = '';
$('#my_selector').each(function(){
    h += '<' + this.NodeName;
    for(var k in this.attributes){
        h += ' ' + this.attributes[k].nodeName + '="' + this.attributes[k].value + '" ';
    }
    h += '>';
    h += this.innerHTML;
    h += '</' + this.NodeName + '>';
});
var h='';
$(“#我的选择器”)。每个(函数(){
h+='';
h+=this.innerHTML;
h+='';
});

好吧,回答我自己的问题

console.log($('<div>').append($myObject.clone()).remove().html());
console.log($('').append($myObject.clone()).remove().html());
感谢John Feminella在这篇文章中所说的话:

谁引用了这篇文章:


在您的示例中,您不需要使用call$myObject.html()吗

但是,既然我猜您想要的可能不仅仅是这个,那么将对象转换为JSON就可以了吗

有几个jQuery插件可以做到这一点。我用过的那个,对我来说一直都很有用,似乎不再有用了,但你可以在这里找到它

对jQuery插件库的搜索给出了这两种可能的替代方法


我从未尝试过这两种方法,因此无法保证它们的有效性

console.log($('').append($myObject.clone()).html());
 console.log($('<div></div>').append($myObject.clone()).html());

$(文档).ready(函数(){
$('#btnCreateNewDiv')。单击(函数(){
$('#result').append($('').append($('#testClone').clone()).html();
$('#result div:last').show();
});
});
克隆
{
背景色:红色;
}
.克隆h1
{
颜色:青柠;
}
测试
​

BenoKrapo嗯,也许我问的问题不太正确。正如你从我的另一个答案中所看到的,我确实找到了一个解决方案,提供了我所寻找的。但是谢谢你提供的信息。我一直在这里学习新东西,所以。而且,这似乎是jQuery将来要添加的一个方便的内置特性。我可以看到它的很多用途。可能重复:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#btnCreateNewDiv').click(function () {
                $('#result').append($('<div></div>').append($('#testClone').clone()).html());
                $('#result div:last').show();
            });
        });
    </script>
    <style type="text/css">
        .Clone
        {
            background-color: Red;
        }

        .Clone h1
        {
            color: Lime;
        }
    </style>
</head>
<body>
    <input type="button" value="Create New Div" id="btnCreateNewDiv" />
    <span id="result"></span>
    <div id="testClone" class="Clone" style="display: none;">
        <h1>
            test
        </h1>
    </div>
    ​
</body>
</html>