最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How To Get Child Elements Of tag 'li' In The 'ul' In javascript - Stack Overflow

programmeradmin2浏览0评论

When getting the list elements in ul, we use get element by class name or tag and are returned in an array

var targetImages = document.getElementById("image-list-view").getElementsByTagName("img");

I need to get the element image in this list and i seem to be stack

<ul id="image-list-view">
    <li>
        <a href="#"  data-gallery>
            <img src="images/pic1.jpg"/>
        </a>
    </li>
    <li>
        <a href="#"  data-gallery>
            <img src="images/pic2.jpg"/>
        </a>
    </li> 
</ul>

EDIT

Sorry i didnt explain the problem with the code.

the code works fine when the site starts but targetImages can no longer be accessed later or referenced ie (images cannot be accessed);

When getting the list elements in ul, we use get element by class name or tag and are returned in an array

var targetImages = document.getElementById("image-list-view").getElementsByTagName("img");

I need to get the element image in this list and i seem to be stack

<ul id="image-list-view">
    <li>
        <a href="#"  data-gallery>
            <img src="images/pic1.jpg"/>
        </a>
    </li>
    <li>
        <a href="#"  data-gallery>
            <img src="images/pic2.jpg"/>
        </a>
    </li> 
</ul>

EDIT

Sorry i didnt explain the problem with the code.

the code works fine when the site starts but targetImages can no longer be accessed later or referenced ie (images cannot be accessed);

Share Improve this question edited Feb 26, 2018 at 9:52 Lucem asked Feb 23, 2018 at 14:44 LucemLucem 3,0824 gold badges22 silver badges37 bronze badges 4
  • I need to get the element image in this list Which one is that? – gurvinder372 Commented Feb 23, 2018 at 14:47
  • developer.mozilla/en-US/docs/Web/API/Document/… will allow you to write the CSS selector you want. – jmargolisvt Commented Feb 23, 2018 at 14:47
  • What's the problem? Looks fine to me, though you could simplify your DOM query with document.querySelectorAll('#image-list-view img') – Patrick Roberts Commented Feb 23, 2018 at 14:50
  • @PatrickRoberts please see my edit. I cannot access the variable after the app is already running. – Lucem Commented Feb 26, 2018 at 9:53
Add a ment  | 

2 Answers 2

Reset to default 4

Use the function querySelectorAll along with this selector ul#image-list-view li img.

This approach allows you to get the image elements straightforward and avoid a second function execution. Further, this approach doesn't return null value if the element with id image-list-view doesn't exist.

var targetImages = document.querySelectorAll("ul#image-list-view li img");
console.log("Count of images: " + targetImages.length);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ul {
  display: none
}
<ul id="image-list-view">
  <li>
    <a href="#" data-gallery>
      <img src="images/pic1.jpg" />
    </a>
  </li>
  <li>
    <a href="#" data-gallery>
      <img src="images/pic2.jpg" />
    </a>
  </li>
</ul>

Resource

  • document.querySelectorAll()

    Returns a list of the elements within the document (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors. The object returned is a NodeList.

Your code works, you just need to loop thru the results and do something with the images.

getElementsByTagName returns an HTMLCollection, which is almost like an array. You can loop thru it the same way.

var targetImages = document.getElementById("image-list-view").getElementsByTagName("img");

for (var i = 0; i < targetImages.length; i++) {
  console.log(targetImages[i]);
}
I need to get the element image in this list and i seem to be stack

<ul id="image-list-view">
  <li>
    <a href="#" data-gallery>
      <img src="images/pic1.jpg" />
    </a>
  </li>
  <li>
    <a href="#" data-gallery>
      <img src="images/pic2.jpg" />
    </a>
  </li>
</ul>

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>