用输入元素jquery替换所有图像

时间:2014-10-29 02:12:17

标签: javascript jquery html

我需要从textarea值中找到所有图像,然后用输入文本元素替换 并且img的src的值是reemplace

之后,在新的div('#newDiv')

上输出最后一个字符串

我的剧本不替换任何我不知道为什么

这是我到目前为止所做的,

<html>
<head></head>
<body>
<textarea id="caja"></textarea>

<input type="button" onClick="parsingHtml()" value="read">


<div id="newDiv"></div>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>

function parsingHtml()
{

  var content = $('#caja').val();
  var newContent = $(content).find("img").replaceWith('<input type="text">');

  alert(newContent)
  $('#newDiv').html(newContent);
};
</body>
</html>

任何想法?提前谢谢!

1 个答案:

答案 0 :(得分:0)

我可以看到两个问题

  1. newContent仅指img元素,而不是完整内容
  2. 由于您将值传递给jQuery,如果该值不以<开头,则它将被视为选择器而不是元素创建命令
  3. 所以

    &#13;
    &#13;
    //dom ready handler
    jQuery(function($) {
      //click event handler registration
      $('#read').click(parsingHtml);
    })
    
    function parsingHtml() {
      var content = $('#caja').val();
      var newContent = $('<div />', {
        html: content
      });
      newContent.find("img").replaceWith('<input type="text">');
    
      console.log(newContent)
      $('#newDiv').html(newContent.contents());
    
    };
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <textarea id="caja"></textarea>
    
    <input type="button" id="read" value="read">
    
    
    <div id="newDiv"></div>
    &#13;
    &#13;
    &#13;

相关问题