6

我正在尝试在单击事件后从我的 js 文件中设置数据 ID 和/或跨度值。

<span id="test"></span>

我的 sudo 代码 js 文件

nextLink: function(event) {
    $('#test').val = 3;
    $('#test').data('id') = 'Next';
},
4

2 回答 2

16

Try setting it as an attribute..

 $('#test').attr('data-id' , 'Next');   // JQuery

You can also try the setAttribute() ..

var d = document.getElementById("test");  //   Javascript
d.setAttribute('data-id' , 'Next');       //

Using this approach will reflect the new attribute in the DOM

In your code You are trying to set the attribute which is not present .. So you need to add the attribute first to add that ..

于 2012-10-24T17:18:15.377 回答
4

You probably need this:

$('#test').data('id', 'Next');
于 2012-10-24T17:20:24.260 回答