Javascript 为什么这个JQuery代码什么都不返回? var newvalue=''; var abc=$(''+newvalue+'').find('*').each(function(){ }).html(); 警报(abc);

Javascript 为什么这个JQuery代码什么都不返回? var newvalue=''; var abc=$(''+newvalue+'').find('*').each(function(){ }).html(); 警报(abc);,javascript,jquery,Javascript,Jquery,我希望abc等于“新价值”。但在我目前的代码中,abc是空的。为什么? 这正是我真正想要做的,但举个例子,我在上面留下了空白: <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> <script> var newvalue = '<img class="youtube_replace youtube_canvas" data-code="Wn-_MyJV37E" src="

我希望abc等于“新价值”。但在我目前的代码中,abc是空的。为什么?

这正是我真正想要做的,但举个例子,我在上面留下了空白:

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
var newvalue = '<img class="youtube_replace youtube_canvas" data-code="Wn-_MyJV37E" src="http://img.youtube.com/vi/Wn-_MyJV37E/0.jpg" />';

var abc = $('<div>' + newvalue + '</div>').find('*').each(function() {
}).html();

alert(abc);

</script>
var白名单=['a','div','img','span'];
var abc=$(''+newvalue+'').find('*').each(function(){
if($.inArray(this.nodeName.toLowerCase(),白名单)=-1){
$(this.remove();
}
}).html()//abc现在已经消毒了!!!
这样做(如果您坚持要获取刚刚生成的元素的HTML):

实际上,您将收到整个
标记的HTML。

这样做(如果您坚持要获取刚刚生成的元素的HTML):

您将实际收到整个
标记的HTML。

将其分解:

var abc = $('<div>' + newvalue + '</div>').find('*').each(function() {
    /* your JS code here */
}).parent().html();
var abc=$(''+newvalue+'')//创建一个包含img元素的div
.find('*')//检索img元素
.each(function(){})//迭代jQuery集(仅一个img元素)
.html()//返回img元素的HTML序列化
//它没有内容,因此是空字符串
您可以调用
.parent().html()
,它将检索您创建的
div
的内容

在第二个示例中,您需要
.end().html()
,它将弹出内部jQuery堆栈并返回到最顶层的
div

分解它:

var abc = $('<div>' + newvalue + '</div>').find('*').each(function() {
    /* your JS code here */
}).parent().html();
var abc=$(''+newvalue+'')//创建一个包含img元素的div
.find('*')//检索img元素
.each(function(){})//迭代jQuery集(仅一个img元素)
.html()//返回img元素的HTML序列化
//它没有内容,因此是空字符串
您可以调用
.parent().html()
,它将检索您创建的
div
的内容


在第二个示例中,您需要
.end().html()
,它将弹出内部jQuery堆栈并返回最顶层的
div

这里的问题是:

var abc = $('<div>' + newvalue + '</div>') //Creates a div with an img element inside it
         .find('*')   //retrieves the img element
         .each(function() {}) //iterates over the jQuery set (only one img element)
         .html();  //returns the HTML serialization of the img element
                   //which has no contents, so it is an empty string

(但是@Dennis先到了:))

这里的问题是,当您这样做时:

var abc = $('<div>' + newvalue + '</div>') //Creates a div with an img element inside it
         .find('*')   //retrieves the img element
         .each(function() {}) //iterates over the jQuery set (only one img element)
         .html();  //returns the HTML serialization of the img element
                   //which has no contents, so it is an empty string

(但是@Dennis先到了:))

您希望
abc
的值与另一个变量的名称相同-
newvalue
?是。但是我当然计划在函数()中做一些事情,{}…现在它只是空的,例如,将html作为字符串处理是不好的。“别这么做!”雷诺斯别无选择。我正在使用node.js。我在后端执行此操作。@user847495为什么飞行f***会在后端使用jQuery。这是有史以来最愚蠢的事情。您希望
abc
的值与另一个变量的名称相同-
newvalue
?是的。但是我当然计划在函数()中做一些事情,{}…现在它只是空的,例如,将html作为字符串处理是不好的。“别这么做!”雷诺斯别无选择。我正在使用node.js。我在后端执行此操作。@user847495为什么飞行f***会在后端使用jQuery。这是有史以来最愚蠢的事情。我需要在“每个”中执行操作。请阅读我的编辑。我需要在“每个”中执行操作。请阅读我的编辑。等等…为什么我要.end()而不是.parent()?当然……在实际情况中,字符串将不仅仅是一个图像标记。所以我应该始终使用“.end”对吗?
end
在所有情况下都有效
parent
是我第一个想到的东西,因为您的第一个示例只有一个元素。等等……为什么我要.end()而不是.parent()?当然……在实际情况中,字符串将不仅仅是一个图像标记。所以我应该始终使用“.end”对吗?
end
在所有情况下都有效
parent
是我第一个想到的东西,因为您的第一个示例只有一个元素。
var abc = $('<div>' + newvalue + '</div>').find('*')
var abc = $('<div>' + newvalue + '</div>')
    .find('*')
    .each(function() {
        // stuff
    })
    .parent().html();