在 JQuery 中更改 td 的 html

发布于 2024-08-26 03:57:49 字数 678 浏览 4 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2024-09-02 03:57:49

你那里有一些混乱的东西。试试这个...

function reorder(selector){
     jQuery(selector)
      .find('tr td:first-child')
      // Remove the + 1 if you want it to start at 0.          
      .html(function(i) { return i + 1; });
}

以下是一些更改...

  • 更改了您的参数变量以使其更加清晰。
  • 您可以使用 jQuery('selector'),而不是 jQuery.find()
  • 您可以使用 html() 方法。

还值得一提的是,如果您没有使用其他 jQuery 库(或不打算使用),则可以使用比 jQuery 短的 $

我还修改了您的 HTML

<form>
    <input type="button" onclick="reorder('#my-table');" value="test"/>
    <table id="my-table">
        <tr><td>1</td></tr>
        <tr><td>1</td></tr>
        <tr><td>1</td></tr>
    </table>
</form>

您可能忘记了使用具有相同值的多个 id 属性不是有效的标记。您可以在那里使用一个类,或者最好完全省略它。您可能不关心有效标记,但当浏览器的 CSS 和 JavaScript 期望 id 属性唯一时,它们可能会做一些有趣的事情(现在或将来)。

You've got a bit of mixed up stuff there. Try this...

function reorder(selector){
     jQuery(selector)
      .find('tr td:first-child')
      // Remove the + 1 if you want it to start at 0.          
      .html(function(i) { return i + 1; });
}

Here are some of the changes...

  • Changed your argument variable to be clearer.
  • You can use jQuery('selector'), not jQuery.find().
  • You can use the 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 than jQuery.

I've also modified your HTML

<form>
    <input type="button" onclick="reorder('#my-table');" value="test"/>
    <table id="my-table">
        <tr><td>1</td></tr>
        <tr><td>1</td></tr>
        <tr><td>1</td></tr>
    </table>
</form>

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 an id attribute to unique.

七度光 2024-09-02 03:57:49

我认为你需要改变我的值增量表达式。

function reorder(targetId){
        var i = 1;
        jQuery.find(targetId).each(function(){
            jQuery(this).attr("innerHTML", i);
            i++; // Before that it was i ++ 
        });
    }

I think you need change i values incrementation expression .

function reorder(targetId){
        var i = 1;
        jQuery.find(targetId).each(function(){
            jQuery(this).attr("innerHTML", i);
            i++; // Before that it was i ++ 
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文