Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome中的childNodes.0长度/IE&;中的正确长度;FF_Javascript_Jquery_Html_Dom - Fatal编程技术网

Javascript Chrome中的childNodes.0长度/IE&;中的正确长度;FF

Javascript Chrome中的childNodes.0长度/IE&;中的正确长度;FF,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有一个孩子,我需要爬网寻找特定的链接div。HTML看起来是这样的: <div id="ctl00_LeftNav"> <h3> <a href="../App_Templates/#" rel="0">Menu 1</a></h3> <div> <p> <span> <div style=

我有一个孩子,我需要爬网寻找特定的链接div。HTML看起来是这样的:

<div id="ctl00_LeftNav">
    <h3>
        <a href="../App_Templates/#" rel="0">Menu 1</a></h3>
    <div>
        <p>
            <span>
                <div style="padding-left: 0px; font-weight: bold;">
                    <a href="javascript:OnLeftMenuSelection(1);">Opt 1</a></div>
                <div style="padding-left: 10px;">
                    <a href="javascript:OnLeftMenuSelection(56);">Opt 1a</a></div>
                <div style="padding-left: 0px; font-weight: bold;">
                    <a href="javascript:OnLeftMenuSelection(2);">Opt 2</a></div>
            </span>
        </p>
    </div>
    <h3>
        <a href="../App_Templates/#" rel="1">Menu 2</a></h3>
    <div>
        <p>
            <span>
                <div style="padding-left: 0px; font-weight: bold;">
                    <a href="javascript:OnLeftMenuSelection(33);">Opt 1</a></div>
                <div style="padding-left: 0px; font-weight: bold;">
                    <a href="javascript:OnLeftMenuSelection(34);">Opt 2</a></div>
                <div style="padding-left: 10px;">
                    <a href="javascript:OnLeftMenuSelection(42);">Opt 2a</a></div>
            </span>
        </p>
    </div>
    <h3>
        <a href="../App_Templates/#" rel="2">Menu 3</a></h3>
    <div>
        <p>
            <span>
                <div style="padding-left: 0px; font-weight: bold;">
                    <a href="javascript:OnLeftMenuSelection(58);">Opt 1</a></div>
                <div style="padding-left: 0px; font-weight: bold;">
                    <a href="javascript:OnLeftMenuSelection(59);">Opt 2</a></div>
            </span>
        </p>
    </div>
</div>

你应该考虑使用jQuery。

$('#ctl00_LeftNav').children("div").each(function(index) {

   alert(index + ': ' + $(this).text()); // displays inner text of div

});
或者像patrick指出的那样,这会得到选择器的所有子div

$('#ctl00_LeftNav > div').each(function(index) {

   alert(index + ': ' + $(this).text()); // displays inner text of div

});

来源:对于循环

如果我正确理解了您的代码,请将整个函数替换为:

function OnPageLoadCategorySelect(Category) {
  $j("#ctl00_LeftNav > div").each(function(Tab, el){
      $j('a', this).each(function(){
          var id = $j(this).attr('href').replace('javascript:OnLeftMenuSelection(','').replace(')','');
          if( id == Category ) {
              SelectedTabIndex = Tab;                        
              OnLeftMenuSelection(Category);
              $j('#ShopTabs').show();
              $j('#ctl00_LeftNav').accordion('activate', Tab);                      
              return false; // exit the loop
          }
      })
  });
}

每个
函数的第一个参数是从零开始的索引。通过提供选项卡,它会为您计算它。

有点-我正在学习jQuery。因此,如果有更好的方法使用它,请让我知道:)有没有任何方法只抓取div而不是h3标签?我该怎么做内部循环呢?我更新了答案,这样它就能抓取选择器的所有子div。哇,这很简单。谢谢-现在就来试一试。此代码将只查找作为
#ctl00_LeftNav
的直接子级的div。它不会穿越更深的后代。@patrick,是的,我知道。我给了他一个基本的想法,通过使用这个基本的想法,他应该能够得到所有他需要的孩子。哇,谢谢道格。我刚开始写我自己的,但当我的失败时,我会试试这个……没用。当我用firebug一步一步地浏览它时,它只是跳过了“$j”(“ctl00”LeftNav>div”)。每个(函数(Tab,el)”。@Chris,我提供的选择器将适用于您提供的HTML。可能是ID已更改,或者HTML不相同(意思是说,
div
不是
\ctl00…
的直接子对象)。如果你查看页面的来源(不是Firebug来源),它与你在上面发布的相同吗?@Chris-除了Doug的建议,你在使用“$j”别名之前是否打过电话到
jQuery.noConflict()
?知道了。是的,它是相同的-id是以“;”结尾的。刚刚替换。替换(“),”;替换为“;”,“);”;。它起作用了!
$('#ctl00_LeftNav > div').each(function(index) {

   alert(index + ': ' + $(this).text()); // displays inner text of div

});
function OnPageLoadCategorySelect(Category) {
  $j("#ctl00_LeftNav > div").each(function(Tab, el){
      $j('a', this).each(function(){
          var id = $j(this).attr('href').replace('javascript:OnLeftMenuSelection(','').replace(')','');
          if( id == Category ) {
              SelectedTabIndex = Tab;                        
              OnLeftMenuSelection(Category);
              $j('#ShopTabs').show();
              $j('#ctl00_LeftNav').accordion('activate', Tab);                      
              return false; // exit the loop
          }
      })
  });
}