在 JQuery 中更改 td 的 html
我想使用 jQuery 将表 td 中的文本从 1,1,1 更改为 1,2,3
但这似乎不起作用。我的代码有什么问题吗?
这是我的 JavaScript 函数。
function reorder(targetId){
var i = 1;
jQuery.find(targetId).each(function(){
jQuery(this).attr("innerHTML", i);
i ++;
});
}
我的html代码。
<form>
<input type="button" onClick="reorder('#index');" value="test"/>
<table>
<tr><td id="index">1</td></tr>
<tr><td id="index">1</td></tr>
<tr><td id="index">1</td></tr>
</table>
</form>
I want to change text in table td from 1,1,1 to 1,2,3 with jQuery
But it not seem to work. what is wrong in my code?
This is my JavaScript function.
function reorder(targetId){
var i = 1;
jQuery.find(targetId).each(function(){
jQuery(this).attr("innerHTML", i);
i ++;
});
}
My html code.
<form>
<input type="button" onClick="reorder('#index');" value="test"/>
<table>
<tr><td id="index">1</td></tr>
<tr><td id="index">1</td></tr>
<tr><td id="index">1</td></tr>
</table>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你那里有一些混乱的东西。试试这个...
以下是一些更改...
jQuery.find()
。html()
方法。还值得一提的是,如果您没有使用其他 jQuery 库(或不打算使用),则可以使用比
jQuery
短的$
。我还修改了您的 HTML
您可能忘记了使用具有相同值的多个 id 属性不是有效的标记。您可以在那里使用一个类,或者最好完全省略它。您可能不关心有效标记,但当浏览器的 CSS 和 JavaScript 期望
id
属性唯一时,它们可能会做一些有趣的事情(现在或将来)。You've got a bit of mixed up stuff there. Try this...
Here are some of the changes...
jQuery.find()
.html()
method.It's worth mentioning too that if you're not using another jQuery library (or don't intend to), you can use
$
which is shorter thanjQuery
.I've also modified your HTML
You may have forgot that using multiple
id
attributes with the same value is not valid markup. You could use a class there, or better still omit it entirely. You may not care about valid markup, but browsers' CSS and JavaScript could do some funky stuff (now or in the future) when they expect anid
attribute to unique.我认为你需要改变我的值增量表达式。
I think you need change i values incrementation expression .