以下是使用数据集api从标记获取数据属性的方法。
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/chrysanthemum" />
请以示例提供答案。
感谢。
答案 0 :(得分:3)
dataset
或data-
Element.setAttribute()
属性
dataset
或setAttribute
。div[data-price]:after
样式。
var div = document.querySelector('div');
var data = div.dataset;
div.innerHTML += ' was ' + data.color;
data.color = 'yellow';
div.innerHTML += '; now it is ' + data.color + '. <br/>';
data.type = 'Golden Delicious';
div.setAttribute('data-price', '$1.00');
div.innerHTML += 'This div has the following attribute/value pairs:';
for (var i = 0; i < div.attributes.length; i++) {
var attr = div.attributes[i];
div.innerHTML += '<br/>' + attr.name + '=' + attr.value;
}
div.innerHTML += '<br/>This div has the following dataset key/value pairs:';
for (var key in data) {
div.innerHTML += '<br/>' + key + '=' + data[key];
}
div[data-color=red] {
color: red;
}
div[data-color=yellow] {
color: goldenrod;
}
div[data-price]:after {
content: attr(data-price);
color: green;
}
<div data-color="red">Apple</div>