Javascript:无法将 href 添加到列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21977349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: can't add href to list item
提问by techtinkerer
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");
回答by xdazz
li
doesn't have the href
attribute, you have to wrap an a
tag inside li
.
li
没有href
属性,你必须在a
里面包装一个标签li
。
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);
回答by duskwuff -inactive-
The href
attribute is not meaningful on an <li>
element. 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.
该href
属性对<li>
元素没有意义。如果要将列表元素制作为链接,则需要将其内容包装在一个<a>
元素中并将 应用href
到该元素中。
回答by Roko C. Buljan
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
标签创建。
回答by user13500
Though solved, I add some more information for you to read :)
虽然已解决,但我添加了更多信息供您阅读:)
Content and attributes
内容和属性
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. Looking at the specswe find it to be:
因此,该<ul>
元素具有内容模型。查看规格,我们发现它是:
- Zero or more liand script-supportingelements.
By this we can conclude that we can not have an anchor, a
, inside the ul
element. But what about adding a href
attribute to the ul
?
由此我们可以得出结论,我们不能a
在ul
元素内部有一个锚点。但是向 中添加一个href
属性ul
呢?
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:
全局属性如下:
- accesskey
- class
- contenteditable
- dir
- draggable
- dropzone
- hidden
- id
- lang
- spellcheck
- style
- tabindex
- title
- translate
- 访问键
- 班级
- 内容可编辑
- 目录
- 可拖动的
- 拖放区
- 隐
- ID
- 朗
- 拼写检查
- 风格
- 标签索引
- 标题
- 翻译
In addition it can have various event handler attributes, like onmouseover
, onclick
, on...
and ARIA attributes. But, as we see, no href
attribute.
此外,它可以有不同的事件处理程序的属性,如onmouseover
,onclick
,on...
和ARIA属性。但是,正如我们所见,没有href
属性。
In conclusion we now know that:
总之,我们现在知道:
ul
can not have an anchor as a child.ul
can not have thehref
attribute.
ul
小时候不能有主播。ul
不能有href
属性。
But, the question was about href
on li
element!
但是,问题是关于href
对li
元素!
As li
and ul
/ ol
are intertwined we first had a look at ul
. For li
we follow the same procedure: The content modelfor li
is:
由于li
和ul
/ol
是交织在一起的我们先来看看ul
。对于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
我们发现:
- Global attributes
If the element is a child of an ol element: value - Ordinal value of the list item
- 全局属性
如果元素是 ol 元素的子元素: value - 列表项的序数值
In other words, we now know:
换句话说,我们现在知道:
ul
can nothavean anchor as a child.ul
can nothavethehref
attribute.li
can havean anchor as a child.li
can nothavethehref
attribute.
ul
能不能有一个锚作为一个孩子。ul
可以不具有的href
属性。li
小时候可以有主播。li
可以不具有的href
属性。
Solution
解决方案
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>