admin管理员组文章数量:1384106
I'm wondering how with javascript can I target a ul within a div, to use javascript to add another li to the ul. The problem is that the div has an id, the ul, and subsequent li's do not.
HTML:
<div id="topnav">
<div id="navcontainer">
<ul>
<li><a href="./" rel="">Hello |</a></li>
<li><a href="services.php" rel="" id="current">We Provide |</a></li>
<li><a href="service_providers.php" rel="">Service Providers</a></li>
</ul>
Ordinarily I would just add an id to the ul, but I with the software I'm using that is not an option, I must use javascript to acplish this task. Thanks in Advance!
I'm wondering how with javascript can I target a ul within a div, to use javascript to add another li to the ul. The problem is that the div has an id, the ul, and subsequent li's do not.
HTML:
<div id="topnav">
<div id="navcontainer">
<ul>
<li><a href="./" rel="">Hello |</a></li>
<li><a href="services.php" rel="" id="current">We Provide |</a></li>
<li><a href="service_providers.php" rel="">Service Providers</a></li>
</ul>
Ordinarily I would just add an id to the ul, but I with the software I'm using that is not an option, I must use javascript to acplish this task. Thanks in Advance!
Share Improve this question asked May 12, 2016 at 17:43 Spencer DavisSpencer Davis 372 silver badges7 bronze badges2 Answers
Reset to default 4try using querySelector
var ulElement = document.querySelector( "#navcontainer ul" )
var myList = document.getElementById('navcontainer').getElementsByTagName('ul')[0];
// or: var myList = document.querySelector('#navcontainer ul');
var myNewListItem = document.createElement('li');
myNewListItem.textContent = "A New Item";
myList.appendChild(myNewListItem);
本文标签: htmlHow to target a ul within a div using JavascriptStack Overflow
版权声明:本文标题:html - How to target a ul within a div using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744454409a2606916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论