如何在动态创建的元素上设置单个属性
我必须动态创建一个 Anchor 标记,我要向其中添加 Foundation 方面.其中一个元素被称为 Foundation 使用的数据工具提示".
I have to dynamically create an Anchor tag that I am adding a Foundation aspect to. One of the elements is called "data-tooltip" that Foundation uses.
如果我尝试创建以下行,我该如何创建data-tooltip"元素?
If I'm trying to create the following row, how do I create the "data-tooltip" element?
我所期待的
<a data-tooltip aria-haspopup="true">FOO</a>
我如何生成元素
var artworkColumn = document.createElement("a");
artworkColumn.setAttribute("aria-haspopup", "true");
我已经在 JQuery 中看到了执行此操作的方法,但是由于a"元素不存在,因此我无法使用 StackOverflow 上描述的方法.
I've seen ways in JQuery to do this, but since the "a" element does not exist, I can't use the methods described here on StackOverflow.
谢谢史蒂夫
只需执行与 aria-haspopup
完全相同的操作,但将值设置为空字符串:
Just do the exact same thing you did for aria-haspopup
, but set the value to an empty string:
artworkColumn.setAttribute("data-tooltip", '');
示例:http://codepen.io/anon/pen/ZQVdQL
我认为可能会让您失望的是,检查元素不会显示您期望的结果,但是如果您将元素打印到控制台,您会发现它实际上具有您期望的属性.
I think the thing that might be throwing you off is that inspecting the element will not show you the result you expect, but if you print out the element to the console you will see that it does in fact have the properties you expect.