Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 - Fatal编程技术网

Jquery 对象的字符串未按预期工作

Jquery 对象的字符串未按预期工作,jquery,Jquery,在JQuery中将字符串转换为html对象时,我没有得到整个片段,我想知道是否正确使用了它 以下是填充“数据”后的代码: 结果如下: 如您所见,在将HTML转换为对象,然后再将其转换为字符串后,只有IMG部分在那里,其余部分不见了。我在这里做错了什么吗?$data.html;参考数据的innerHtml,这将在您的案例中返回img 使用此$data.html;您并没有将数据转换为html,您只是要求jquery返回div中的html 您要求-String对象不能按预期工作 您的数据变量已经是一个

在JQuery中将字符串转换为html对象时,我没有得到整个片段,我想知道是否正确使用了它

以下是填充“数据”后的代码:

结果如下:

如您所见,在将HTML转换为对象,然后再将其转换为字符串后,只有IMG部分在那里,其余部分不见了。我在这里做错了什么吗?

$data.html;参考数据的innerHtml,这将在您的案例中返回img

使用此$data.html;您并没有将数据转换为html,您只是要求jquery返回div中的html

您要求-String对象不能按预期工作

您的数据变量已经是一个对象


阅读更多关于

一个打印变量数据的实际内容,即整个字符串,而.html检索div的内部html


html方法将引用匹配元素的innerHTML。如果您想要outerHTML,可以尝试以下操作:console.log$data.wrap.parent.html;尽管这取决于您到底需要什么,但您最好只引用数据本身?例如,包装数据节点只是为了能够选择它,可能会给CSS选择器带来其他问题。

可能重复做一些不同的事情只是让我困惑了一秒钟。谢谢。@Dave:当您将数据包装到jQuery对象中时,jQuery将把这种情况下的内容视为对包装在jQuery对象中的DOM元素的任何其他引用。因此,在调用.html时,您可以从div内部获取html。calling.html与使用console.log$data[0]时是一样的;这也只是给你的div的内容。谢谢,不敢相信我错过了。
    console.log(data);
    console.log("---");
    console.log($(data).html());
<div style='width:260px; height:128px; text-align:left' id='productTn'><img src='images/rProduct02.jpg' id='productTnImg' class='productTnImg' alt='Selected Product' style='border-style:none' height=128 width=128 /></div><div id='productLI' style='width:281px; min-height:185px'><b>Audi</b></div>
--- 
<img src="images/rProduct02.jpg" id="productTnImg" class="productTnImg" alt="Selected Product" style="border-style:none" height="128" width="128"> 
console.log(data); // prints the actual content of data
console.log($(data).html()); // prints the inner HTML of data