【问题标题】:Before append text in div jquery在 div jquery 中追加文本之前
【发布时间】:2012-03-01 19:04:26
【问题描述】:

在已附加的 div id "txt" 中附加文本。我使用了 appendChild() 函数,添加了“测试这是”但我需要“这是测试”

document.getElementById("txt").appendChild("This is");

之前

测试

之后

这是测试

帮帮我

【问题讨论】:

    标签: jquery html append appendchild prepend


    【解决方案1】:

    您在问题的标签中找到了答案——看看.prepend() jQuery 函数。它不是在最后将子节点添加到元素,而是在开头添加它们。假设#txt是一个原本只包含文本节点'Testing'的元素,那么这段代码:

    $('#txt').prepend('This is ');
    

    会给你想要的结果。

    Working DEMO

    【讨论】:

      【解决方案2】:

      你可以这样做:

      var old_string = document.getElementById("div_id").innerHTML;
      var new_string = "is Testing";
      document.getElementById("div_id").innerHTML = new_string + " " + old_string;
      

      希望对您有所帮助。

      【讨论】:

        【解决方案3】:
        $("#txt").before("before text");
        $("#txt").after("after text");
        

        DEMO

        或者你可以从链接中受益

        $("#txt").before("before ").after("after ");
        

        【讨论】:

        • 作为相同的 div "txt" 加载图像之前附加。如何附加图片
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-29
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 2011-04-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多