简体   繁体   English

Javascript:无法将 href 添加到列表项

[英]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 DEMO.演示。

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 :)虽然已解决,但我添加了更多信息供您阅读:)

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.因此<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:全局属性如下:

  • accesskey访问密钥
  • class班级
  • contenteditable内容可编辑
  • dir目录
  • draggable可拖动
  • dropzone拖放区
  • hidden
  • id ID
  • lang
  • spellcheck拼写检查
  • style风格
  • tabindex标签索引
  • title标题
  • translate翻译

In addition it can have various event handler attributes , like onmouseover , onclick , on... andARIA attributes .此外,它还可以具有各种事件处理程序属性,例如onmouseoveronclickon...ARIA 属性 But, as we see, no href attribute.但是,正如我们所见,没有href属性。

In conclusion we now know that:总之,我们现在知道:

  1. ul can not have an anchor as a child. ul小时候不能有主播。
  2. ul can not have the href attribute. ul不能有href属性。

But, the question was about href on li element!但是,问题是关于li元素的href

As li and ul / ol are intertwined we first had a look at ul .由于liul / 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我们发现:

  • 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:换句话说,我们现在知道:

  1. ul can not have an anchor as a child. ul小时候不能主播。
  2. ul can not have the href attribute. ul不能href属性。
  3. li can have an anchor as a child. li可以当主播。
  4. li can not have the href attribute. 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>

jsBin jsbin

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM