Javascript find()不';不能在html变量中工作
我有一个非常简单的问题,并且一直在寻找更复杂的类似问题的答案。我试图替换加载的html中的图像链接,并决定最好是使用Javascript find()不';不能在html变量中工作,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,我有一个非常简单的问题,并且一直在寻找更复杂的类似问题的答案。我试图替换加载的html中的图像链接,并决定最好是使用.get()将html读入字符串变量loadedHTML,如下所示: $.get(loadURL, function(loadedHTML) { myFunction(loadedHTML); }, 'html'); 在myFunction中,我想对加载的html进行一些更改,并最终返回它。我无法让.find()工作。下面是该代码的外观: function myFunct
.get()
将html读入字符串变量loadedHTML
,如下所示:
$.get(loadURL, function(loadedHTML) {
myFunction(loadedHTML);
}, 'html');
在myFunction中,我想对加载的html进行一些更改,并最终返回它。我无法让.find()
工作。下面是该代码的外观:
function myFunction( html ) {
var $html = $("<div>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
$html.find("img", function() {
console.log("found an image"); // doesn't work :(
});
}
函数myFunction(html){
var$html=$(“”+html+“”);
console.log(“$html.html()=”+$html.html());//有效!
$html.find(“img”,function()){
console.log(“找到图像”);//不起作用:(
});
}
我正在用一些很简单的东西自杀。请让我知道我有多笨…我几乎可以肯定你不能用你现有的方式使用
查找
尝试以下方法:
var $foundImages = $html.find("img");
console.log($foundImages.length);
理论上,这将输出找到的图像的数量。我几乎可以肯定,您不能以现有的方式使用find
尝试以下方法:
var $foundImages = $html.find("img");
console.log($foundImages.length);
理论上,它将输出找到的图像数。查找方法没有第二个参数:
你应该试试这个:
function myFunction( html ) {
var $html = $("<div>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
console.log($html.find("img"));
}
函数myFunction(html){
var$html=$(“”+html+“”);
console.log(“$html.html()=”+$html.html());//有效!
log($html.find(“img”);
}
查找方法没有第二个参数:
你应该试试这个:
function myFunction( html ) {
var $html = $("<div>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
console.log($html.find("img"));
}
函数myFunction(html){
var$html=$(“”+html+“”);
console.log(“$html.html()=”+$html.html());//有效!
log($html.find(“img”);
}
.find()
在jquery中没有回调函数。它只有用于选择器、元素、jqueryObject
的参数。您必须像这样检查长度或条件
if($html.find("img").length > 0){
// do stuff here
}
或
.find()
在jquery中没有回调函数。它只有选择器、元素、jqueryObject的参数。
您必须检查长度或类似的条件
if($html.find("img").length > 0){
// do stuff here
}
或
您可以使用此.filter()
:
或者使用.map()
将其制成一个数组:
您可以使用此.filter()
:
或者使用.map()
将其制成一个数组:
只需将id分配给div标记
如下图所示
var $html = $("<div id='placeholder'>" + html + "</div>");
$("#placeholder").find("img", function() {
console.log("found an image"); // doesn't work :(
});
您的结果代码
function myFunction( html ) {
var $html = $("<div id='placeholder'>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
$("#placeholder").find("img", function() {
console.log("found an image"); // doesn't work :(
});
函数myFunction(html){
var$html=$(“”+html+“”);
console.log(“$html.html()=”+$html.html());//有效!
$(“#占位符”).find(“img”,function(){
console.log(“找到图像”);//不起作用:(
});
}只需将id分配给div标签即可
如下图所示
var $html = $("<div id='placeholder'>" + html + "</div>");
$("#placeholder").find("img", function() {
console.log("found an image"); // doesn't work :(
});
您的结果代码
function myFunction( html ) {
var $html = $("<div id='placeholder'>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
$("#placeholder").find("img", function() {
console.log("found an image"); // doesn't work :(
});
函数myFunction(html){
var$html=$(“”+html+“”);
console.log(“$html.html()=”+$html.html());//有效!
$(“#占位符”).find(“img”,function(){
console.log(“找到图像”);//不起作用:(
});
}没有回调,但您可以扩展jQuery以执行您想要的操作:
jQuery.fn.extend({
findEach: function (selector, callback) {
var found = this.find(selector);
if (typeof callback == 'function' && found.length > 0) {
found.each(callback);
}
return found;
}
});
然后像你期望的那样使用:
$html.findEach("img", function(key, value) {//will run for each image
console.log(key);
console.log(value);
console.log(this);
});
没有回调,但您可以扩展jQuery以执行所需操作:
jQuery.fn.extend({
findEach: function (selector, callback) {
var found = this.find(selector);
if (typeof callback == 'function' && found.length > 0) {
found.each(callback);
}
return found;
}
});
然后像你期望的那样使用:
$html.findEach("img", function(key, value) {//will run for each image
console.log(key);
console.log(value);
console.log(this);
});
find()没有回调,但可能值得扩展find()没有回调,但可能值得扩展问题是我需要编辑这些元素的属性,而不仅仅是查看有多少个。然后使用$.each()问题是我需要编辑这些元素的属性,而不仅仅是查看有多少。然后使用$.each()遍历每个元素。