gpt4 book ai didi

javascript - 如何在 jquery 中的 find() 中动态提供元素/标签

转载 作者:行者123 更新时间:2023-11-30 18:21:17 25 4
gpt4 key购买 nike

我已经创建了几个带有 id 的 div,例如 window1、window2 等等。现在我只想从上面创建的 div 中找到

标签。我在 for 循环中执行此操作,但它对我不起作用。这是我正在做的

        for(connectWindow=1;connectWindow<=xmlLength;connectWindow++)
{

//look for the to tag inside the html
var windo = "window"+connectWindow;
var to = "to"+connectWindow;
alert("Making connections" + windo +to)
//$("div#windo").find('strong#to')(function())
$("div#windo").find('p#to').each(function(){
alert("@@@@@@@@@@@@@@@@@@@@");
var name = $(this).text();
//display_function(name,country);
alert("Name is :::"+name);

});
}

请让我知道我要去哪里错了。也请让我知道 JavaScript 中是否有任何解决方案。谢谢!

最佳答案

你需要这样做

 $("div#" + windo).find('p#' + to).each(function(){ // <-- this uses your variable
alert("@@@@@@@@@@@@@@@@@@@@");
var name = $(this).text();
//display_function(name,country);
alert("Name is :::"+name);
});

您的代码查找 id="window"id="to" 而不是您的变量

$("div#windo").find('p#to') 

因为您使用的是 #(id 选择器)

,所以您真的可以通过 ID 来完成
$("#" + windo).find('#' + to)

关于javascript - 如何在 jquery 中的 find() 中动态提供元素/标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11978196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com