Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
将_top添加到特定div-jQuery内的标记_Jquery_Html_Attr - Fatal编程技术网

将_top添加到特定div-jQuery内的标记

将_top添加到特定div-jQuery内的标记,jquery,html,attr,Jquery,Html,Attr,例如,我在几个嵌套的div标记中有几个a标记 <div id="top_level_div"> <div id="inner_level_div"> <a href="http://yahoo.com>text</a> </div> <div id="inner_level_div1> <a href="http://yahoo.com>text</a

例如,我在几个嵌套的div标记中有几个a标记

<div id="top_level_div">
    <div id="inner_level_div">
        <a href="http://yahoo.com>text</a>
    </div>
    <div id="inner_level_div1>
        <a href="http://yahoo.com>text</a>
    </div>
</div>

像这样:

   $("#top_level_div a").attr("target","_top");
#top_level_div a
将获得ID=top_level_div的元素的所有锚定标记后代:

$('#top_level_div a').attr('target', '_top');

    • 像这样的东西怎么样:

      $("#top_level_div a").attr("target","_top");
      
      .attr()的文档

      试试看


      我应该在哪里添加此代码。我尝试在不同的地方添加它,但似乎不起作用
      $('#top_level_div a').attr('target', '_top');
      
      $("#top_level_div a").attr("target","_top");
      
      //select the root element
      //then find all the `a` elements that are descendants of the root element
      //then change that set of elements `target` attribute to `_top`
      $('#top_level_div').find('a').attr('target', '_top');
      
      $("#top_level_div a").attr("target","_top")
      
      $('#top_level_div').find('a').attr('target', '_top');