如何在动态创建的元素上设置单个属性 [英] How to set single attribute on dynamically created element

查看:24
本文介绍了如何在动态创建的元素上设置单个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须动态创建一个 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");

我已经在 J​​Query 中看到了执行此操作的方法,但是由于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.

这篇关于如何在动态创建的元素上设置单个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆