獲取React.js自定義屬性必須data- ,比如data-index 然后用event.target.getAttribute("data-index")
或者console.log(this.refs.myInput.getAttribute("data-index"));獲取
例:
<input type="text" ref="myInput" id='dongtao' data-dd='ddd'/> ;
console.log(event.target.getAttribute("data-index"))
console.log(this.refs.myInput.value);
console.log(this.refs.myInput.id);
console.log(this.refs.myInput.getAttribute("data-dd"));
console.log($('#dongtao').val());
注:
父節點有屬性data-cc
這時候點擊父節點獲取屬性
正確方式是
event.currentTarget.getAttribute(“data-cc”)
而不是
event.target.getAttribute(“data-cc”)
ps:
1.
currentTarget 返回其事件監聽器觸發該事件的元素。
target 返回觸發此事件的元素(事件的目標節點)。
2.react添加屬性必須是 data-xx 形式