javascript - 如何在父兄弟跨度上设置文本

标签 javascript jquery

我有表格,我必须编辑第二列,所以当我单击第二列中的一个单元格时,将显示带有 2 个图像的文本框。

在文本框中输入值并单击 editCost 图像后,我必须在跨度中设置文本。但我不能。

HTML

<table class="gs">
    <thead>
        <tr role="row">
        <th>Name</th>
        <th>Cost in (USD)</th>
        </tr>
    </thead>

<tbody role="alert" aria-live="polite" aria-relevant="all"><tr class="odd">
                <td class=" sorting_1">
                    Culture 
                </td>

                <td align="right" class=" ">
                    <span></span>
                    <span> 
                        <input type="text" name="Culture" value="" id="Culture"> 
                        <img  alt="editCost">
                        <img  alt="cancelCost">
                    </span>
                </td>

            </tr><tr class="even">
                <td class=" sorting_1">
                    RedBloodCell 
                </td>

                <td align="right" class=" ">
                    <span>100</span>
                    <span> 
                        <input type="text" name="RedBloodCell" value="" id="RedBloodCell"> 
                        <img  alt="editCost">
                        <img  alt="cancelCost">
                    </span>
                </td>

            </tr><tr class="odd">
                <td class=" sorting_1">
                    WhiteBloodCell 
                </td>

                <td align="right" class=" ">
                    <span>100</span>
                    <span> 
                        <input type="text" name="WhiteBloodCell" value="" id="WhiteBloodCell"> 
                        <img  alt="editCost">
                        <img  alt="cancelCost">
                    </span>
                </td>

            </tr><tr class="odd even">
                <td class=" sorting_1">
                    WhiteBloodCell 
                </td>

                <td align="right" class=" ">
                    <span>100</span>
                    <span> 
                        <input type="text" name="WhiteBloodCell" value="" id="WhiteBloodCell"> 
                        <img  alt="editCost">
                        <img  alt="cancelCost">
                    </span>
                </td>

            </tr>
</tbody>
</table>

脚本

jQuery(document).ready(function() {
    jQuery('td span:nth-child(2)').hide();

    jQuery("table.gs tbody tr td:nth-child(2)").click(function() {
        jQuery('td span:nth-child(2)').hide();
        jQuery(this).find("span:nth-child(2)").show();
        var value = jQuery.trim(jQuery(this).text());
        jQuery(this).find("input").val(value);
        jQuery(this).find("span:nth-child(1)").text('');
    });

    jQuery('img[alt="editCost"]').click(function() {
        var value = jQuery(this).siblings("input[type=text]").val();
        jQuery(this).parent().siblings("span").text(value);
        jQuery(this).parent().hide(1);      
    });

    jQuery('img[alt="cancelCost"]').click(function() {
        var value = jQuery(this).siblings("input[type=text]").val();
        jQuery(this).parent().siblings("span").text(value);
        jQuery(this).parent().hide(1);      
    });

});

最佳答案

问题是,当点击处理程序在 img[alt="editCost"] 上触发时,事件传播并在 table.gs tbody tr td:nth- 上触发点击处理程序child(2),清空跨度的文本。

event.stopPropagation() 添加到您的 img[alt="editCost"] 点击处理程序:

jQuery('img[alt="editCost"]').click(function(e) {
    e.stopPropagation();
    // your code
});

Fiddle

关于javascript - 如何在父兄弟跨度上设置文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19027837/

相关文章:

javascript - php脚本执行倒计时

javascript - 使函数参数超出范围的问题

javascript - 将 scrollIntoView 函数应用于 JQuery 函数

javascript - Jquery 隐藏和显示在悬停时不起作用

javascript - 模式中的 Twitter bootstrap href 不起作用

javascript - 如何查找对象的元素以从同一对象返回不同的元素

javascript - Jquery Datatable 排序不适用于 html 元素

javascript - jQuery innerwidth/innerheight 无法在 Chrome/Safari 中正确处理图像

javascript - Enter 键创建新行并聚焦当前输入字段

javascript - 更改默认的 Google Maps Api 默认缩放控件图像?