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

How to provide elements/tags dynamically inside the find() in jquery

本文关键字:动态 元素 标签 jquery 中的 find      更新时间:2023-09-26

>我已经用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)

好吧,你需要实际使用这些变量:

$("div#" + windo).find('p#' + to).each(function(){

顺便说一下,jQuery是用JavaScript编写的。如果你使用的是jQuery,你就是在使用JavaScript。