首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不能追加到节点

不能追加到节点
EN

Stack Overflow用户
提问于 2018-09-05 20:48:47
回答 3查看 123关注 0票数 1

表中有以下html代码:

代码语言:javascript
运行
复制
<td id="description">
    <input id="newdescripion" value="VOIP/DATA" type="text">
    <button type="button" id="removeinput">Remove</button>
</td>

单击该按钮时,我希望清空td并添加存储在cookie中的文本。td清空很好,但我无法附加文本。文本在变量中,因为它在警报中可见。我已经使用了下面的代码来尝试并实现这一点,注释掉的代码就是我尝试过的,但不起作用。

代码语言:javascript
运行
复制
$(document).on('click', '#removeinput', function() {
    var hostname =  $('#hostname').text();
    //alert(hostname);
    var trid = $(this).closest('tr').attr('id');
    //alert(trid);
    var olddesc = Cookies.get(hostname+','+trid+',description');
    alert(olddesc);
    $(this).closest('td').empty(); <----- THIS WORKS
    $(this).closest('td').append(olddesc);
    // $(this).closest('tr').find('#description').text(olddesc);
    // $(this).closest('td').text(olddesc);
    // $('#'+trid+' td').each(function(){
    //     if($(this).attr('id') == 'description'){
    //         $(this).append(olddesc);
    //     }
    // })
    //$(document).find('#'+trid+' td#description').append(olddesc);
})

有人能帮我解决这个问题或者推荐一个更好的方法吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-09-05 21:10:11

$(this).closest('td').append(olddesc);在您从td中删除this之后运行,因此td不再是this的祖先。您不需要清空td;只需将其文本设置为olddesc,它将作为设置文本过程的一部分自动清空。

代码语言:javascript
运行
复制
// (REMOVE THIS) $(this).closest('td').empty(); <----- THIS WORKS 
$(this).closest('td').text(olddesc);
票数 0
EN

Stack Overflow用户

发布于 2018-09-05 20:54:03

您可以使用.html()将动态数据添加到id标记中。

代码语言:javascript
运行
复制
 var olddesc = Cookies.get(hostname+','+trid+',description');
 alert(olddesc);

 // Create custom / dynamic HTML
 var temp = `<p>` + olddesc + `</p>`;

 $(this).closest('td').empty(); <----- THIS WORKS

 // Edit: Use ID of html tag
 $('#description').html(temp);

这对你来说是可行的。

票数 2
EN

Stack Overflow用户

发布于 2018-09-05 21:19:04

只需使用.html

代码语言:javascript
运行
复制
    $(document).on('click', '#removeinput', function() {
        var hostname =  $('#hostname').text();
        var olddesc = "Cokkies return"; //Cookies.get(hostname+','+trid+',description');
        $(this).closest('td').html(olddesc); //<----- THIS WORKS
    })
代码语言:javascript
运行
复制
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  
<table>
<tr>
    <td id="description">
        <input id="newdescripion" value="VOIP/DATA" type="text">
        <button type="button" id="removeinput">Remove</button>
    </td>
</tr>
</table>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52193187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档