【问题标题】:How to replace strike tag to input type text? [duplicate]如何替换罢工标签以输入类型文本? [复制]
【发布时间】:2014-01-06 05:13:57
【问题描述】:

如何替换打击元素来输入文字? 我目前不知道是否有任何解决方案,请在这里告诉我!

HTML 文本

In the <b>joy</b> of <strike>other</strike>, lies our <strike>own</strike>.

替换文字:

In the <b>joy</b> of <input type="text">, lies our <input type="text">.

【问题讨论】:

标签: javascript jquery


【解决方案1】:

如果我理解你的问题,你想要这个吗?

$('strike').each(function(){
    $(this).replaceWith("<input type=\"text\" value=\"" + $(this).text() + "\" />");

});

jsfiddle:http://jsfiddle.net/M88u4/

【讨论】:

  • 不使用每次迭代也可以吗?我想在这种情况下使用 for 循环。
  • 你为什么不想使用each?我的意思是,如果您已经在使用其他 jQuery 函数来替换罢工标签,为什么不使用each?你能解释一下为什么要使用 for() 循环吗?
【解决方案2】:

只需添加以下 jQuery 语句

 // search all strike element and replace it 
 $('strike').replaceWith(function(){
        //create input element
        var input = $('<input>',{
            type: 'text',
            value: $(this).html()
        });
        //replace with input element
        return input;
 });

【讨论】:

    【解决方案3】:

    试试这个:

    HTML

    <div id="test">
        In the <b>joy</b> of <strike>other</strike>, lies our <strike>own</strike>.
    </div>
    

    Javascript

    $(function(){
        var strikes = $('#test').children('strike');
        var inputText = "<input type='text' />";
        strikes.each(function(){
            $(this).replaceWith(inputText);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多