如何使用javascript

时间:2016-08-22 00:21:45

标签: javascript jquery string dom attributes

我想使用id:

获取一个元素
var a = document.getElementById("hello");

然后动态添加一个名为“labelText”的属性,并为其赋值“{labeltext}”。

我需要使用连接吗?

我尝试了以下但是没有用:

document.getElementById("hello").setAttribute("labelText", "'{' + labelText + '}' ");

1 个答案:

答案 0 :(得分:1)

你正在做的事情基本上是有效的,首先由Vohuman明确指出的语法错误得到纠正。



$ cf ic cpi gitlab/gitlab-ce:8.10.7-ce.0 registry.eu-gb.bluemix.net/gitlab-ce:8.10.7-ce.0
FAILED
The IBM Containers CLI must be initialized. Run "cf ic init" to initialize it.


$ cf ic images
FAILED
The IBM Containers CLI must be initialized. Run "cf ic init" to initialize it.

//Assign the Attribute:
var labelText='Some Value';
document.getElementById("hello").setAttribute('labelText', '{' + labelText + '}' );

//Define some variables for getting the results
var attributeEl = document.getElementById("attribute");
var theHTMLEl = document.getElementById("theHTML");

///Get the attribute value
var textLabelValue = document.getElementById("hello").getAttribute('labelText');

//Show the results:
attributeEl.textContent = textLabelValue;
theHTMLEl.textContent = document.getElementById("hello").outerHTML;




相关问题