[英]Javascript: can't add href to list item
I am trying to add a new item to a list item.我正在尝试向列表项添加新项。 But the below code isn't adding Hyperlink to the list item I want.但是下面的代码没有将超链接添加到我想要的列表项中。 Can someone please advise what's wrong?有人可以请教有什么问题吗?
HTML: HTML:
<div>
<ul id="list1">
<li>Ut enim ad minim veniam.</li>
<li>Excepteur sint occaecat cupidatat non proident.</li>
</ul>
</div>
JavaScript: JavaScript:
//create new li element
var newListItem = document.createElement("li");
newListItem.textContent = "...ooo";
var ulist = document.getElementById("list1");
console.log("adding link..");
newListItem.setAttribute('href', "http://www.msn.com");
ulist.appendChild(newListItem);
console.log("added item");
li
doesn't have the href
attribute, you have to wrap an a
tag inside li
. li
没有href
属性,您必须在li
内包装一个a
标签。
var a = document.createElement("a");
var ulist = document.getElementById("list1");
var newItem = document.createElement("li");
a.textContent = "...ooo";
a.setAttribute('href', "http://www.msn.com");
newItem.appendChild(a);
ulist.appendChild(newItem);
The href
attribute is not meaningful on an <li>
element. href
属性在<li>
元素上没有意义。 If you want to make an list element into a link, you will need to wrap its contents in an <a>
element and apply the href
to that.如果你想将一个列表元素变成一个链接,你需要将它的内容包装在一个<a>
元素中,并对其应用href
。
Though solved, I add some more information for you to read :)虽然已解决,但我添加了更多信息供您阅读:)
Each element has a content model :每个元素都有一个内容模型:
``[…] a description of the element's expected contents. ``[...] 元素预期内容的描述。 An HTML element must have contents that match the requirements described in the element's content model.[…]'' HTML 元素必须具有与元素内容模型中描述的要求相匹配的内容。[...]''
As such the <ul>
element has a content model.因此<ul>
元素有一个内容模型。 Looking atthe specs we find it to be:查看规格,我们发现它是:
By this we can conclude that we can not have an anchor, a
, inside the ul
element.由此我们可以得出结论,我们不能在ul
元素内有一个锚点a
。 But what about adding a href
attribute to the ul
?但是如何向ul
添加href
属性呢?
Then we look at the Content attributes .然后我们看一下内容属性。
A normative list of attributes that may be specified on the element (except where otherwise disallowed), along with non-normative descriptions of those attributes.可以在元素上指定的规范属性列表(除非另有禁止),以及这些属性的非规范描述。 (The content to the left of the dash is normative, the content to the right of the dash is not.) (破折号左边的内容是规范的,破折号右边的内容不是。)
For ul
we find:对于ul
我们发现:
The Global attributes are the following:全局属性如下:
In addition it can have various event handler attributes , like onmouseover
, onclick
, on...
andARIA attributes .此外,它还可以具有各种事件处理程序属性,例如onmouseover
、 onclick
、 on...
和ARIA 属性。 But, as we see, no href
attribute.但是,正如我们所见,没有href
属性。
In conclusion we now know that:总之,我们现在知道:
ul
can not have an anchor as a child. ul
小时候不能有主播。ul
can not have the href
attribute. ul
不能有href
属性。href
on li
element!但是,问题是关于li
元素的href
! As li
and ul
/ ol
are intertwined we first had a look at ul
.由于li
和ul
/ ol
交织在一起,我们首先看看ul
。 For li
we follow the same procedure: Thecontent model for li
is:对于li
我们遵循相同的程序:li
的内容模型是:
Now, that opens up a wide range of possibilities.现在,这开辟了广泛的可能性。 Here we find a
at top of the list.在这里,我们在列表的顶部找到a
。
And what about the attributes ?那么属性呢? For li
we find:对于li
我们发现:
In other words, we now know:换句话说,我们现在知道:
ul
can not have an anchor as a child. ul
小时候不能有主播。ul
can not have the href
attribute. ul
不能有href
属性。li
can have an anchor as a child. li
可以当主播。li
can not have the href
attribute. li
不能有href
属性。As pointed out by others, is to add it to an anchor that we put as a child of a li
element:正如其他人所指出的,是将它添加到我们作为li
元素的子元素放置的锚点中:
<ul>
<li><a href="myurl">Hello</a></li>
</ul>
var ulist = document.getElementById("list1");
var newListItem = document.createElement("li");
var newAnchor = document.createElement("a");
newAnchor.textContent = "...ooo";
newAnchor.setAttribute('href', "http://www.msn.com");
newListItem.appendChild(newAnchor);
ulist.appendChild(newListItem);
yep so basically you missed the anchor
tag creation.是的,所以基本上你错过了anchor
标签的创建。
Another way would be to simply use the link() method.另一种方法是简单地使用 link() 方法。 For example:例如:
//create new li element
let newListItem = document.createElement("li");
let ulist = document.getElementById("list1");
newListItem.innerHTML = "...ooo".link("http://www.msn.com");
ulist.appendChild(newListItem);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.