1

我已经创建了几个具有 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 中是否有任何解决方案。谢谢 !

4

2 回答 2

1

你需要这样做

 $("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"andid="to"而不是您的变量

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

你真的可以通过 ID 来完成,因为你使用的是#(id 选择器)

$("#" + windo).find('#' + to)
于 2012-08-15T22:24:30.837 回答
1

好吧,您需要实际使用变量:

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

顺便说一句 - jQuery是用JavaScript 编写的。如果您使用的是 jQuery,那么您使用的是 JavaScript。

于 2012-08-15T22:24:39.637 回答