Javascript更新属性

时间:2015-04-16 14:26:41

标签: javascript html xcode html5 attributes

   <input type="text" name="araButon" onKeyUp="return arama()" class="form-control filter" data-filter="app" id="focusedInput"/>

我想通过javascript更新'data-filter'值 我试过这段代码:

  

var gelenText = document.getElementById(“focusedInput”);         的document.getElementById( “focusedInput”)的getAttribute( “数据过滤器”)= gelenText.value;




有人能帮我吗?

2 个答案:

答案 0 :(得分:2)

您可以使用datasetsetAttribute

// dataset
document.getElementById('focusedInput').dataset.filter = "test";

// setAttribute
document.getElementById('focusedInput').setAttribute('data-filter', 'test-1');
<input type="text" name="araButon" onKeyUp="return arama()" class="form-control filter" data-filter="app" id="focusedInput" />

答案 1 :(得分:0)

这是你想要做的事情:

var focusedInput = document.getElementById("focusedInput");
var dataFilter = focusedInput.getAttribute("data-filter");
focusedInput.value = dataFilter;

https://jsfiddle.net/ha6jy2L9/1/