简体   繁体   English

查找并用jQuery替换HTML

[英]Find and replace HTML with jQuery

I am trying to do a Bookmarklet or Browser extension that will modify another website when I view it, more specifically I am trying to use a Javascript Markdown editor with button bar like Stackexchange uses on another website that I do not own so I must do it with a bookmarklet or extension. 我正在尝试做一个Bookmarklet或Browser扩展程序,当我查看它时会修改另一个网站,更具体地说,我想使用带有按钮栏的Javascript Markdown编辑器,例如Stackexchange在我不拥有的另一个网站上使用,所以我必须这样做带有书签或扩展名。

Right now I need to find the textarea on a page and replace it with a different block of code like below. 现在,我需要在页面上找到文本区域,并将其替换为如下所示的其他代码块。 The page does have jQuery so I can use that... IF you can show me how to do this I would really appreciate it, thanks 该页面确实包含jQuery,所以我可以使用它...如果您可以向我展示如何做到这一点,我将非常感谢

Find

<textarea rows="10" cols="50" name="description" id="description" tabindex="4"></textarea>

Replace with... 用。。。来代替...

<div class="wmd-panel">
    <div id="wmd-button-bar"></div>
    <textarea class="wmd-input" id="wmd-input" name="description"></textarea>
</div>

<div id="wmd-preview" class="wmd-panel wmd-preview"></div>

Sure. 当然。 we just need to simply replace the HTML, like so: 我们只需要简单地替换HTML,就像这样:

var htmlToReplace = ' \
  <div class="wmd-panel"> \
    <div id="wmd-button-bar"></div> \
      <textarea class="wmd-input" id="wmd-input" name="description"></textarea> \
  </div> \
  <div id="wmd-preview" class="wmd-panel wmd-preview"></div>';

$("#description").replaceWith(htmlToReplace)

And that'll do the trick for you. 这将为您解决问题。

Check out .replaceWith() 检出.replaceWith()

http://api.jquery.com/replaceWith/ http://api.jquery.com/replaceWith/

$("#description").replaceWith('ALL YOUR CODE');

You can use more Selectors if you need to match attributes or other characteristics 如果需要匹配属性或其他特征,则可以使用更多选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM