开发者

How can I find a root div with jQuery?

开发者 https://www.devze.com 2023-02-15 16:56 出处:网络
I use the jQuery function find() to extract a div of a html file. I use it in that way data.find(\'#tpl_header\')

I use the jQuery function find() to extract a div of a html file. I use it in that way

data.find('#tpl_header')

Problem is开发者_如何学JAVA jquery find() find only non root elements. So this wont work:

[...]
<body>
   <div id="tpl_header" class="table header">
      <div class="tr">
      </div>
   </div>
</body>
</html>

But this way works:

[...]
<body>
   <div id="template"> <!-- because jQuery find function did not find root elements! -->

       <div id="tpl_header" class="table header">
          <div class="tr">
          </div>
       </div>
   </div>
</body>
</html>

Is there a way to find this template div without adding a additional not really needed div?

[ADD]

The template reading function - already with the changes mention below by Sjoerd:

function LoadTemplate()
        {
            $.get('templates/' + template + '/main.html',  
                function(data) {
                    data = $(data);
                    $('#header').html($('#tpl_header', data));
            });
        }


var templateElement = $('#tpl_header')

element.find() only finds descendants of that element, whereas $() finds elements on the whole page.


Another thread gives me a working solution. I have to use the .filter() function to get the root div.

Source: how can get attributes of root element?

0

精彩评论

暂无评论...
验证码 换一张
取 消