将任何纯文本替换为特定div的类 [英] Replace any plain-text to class of specific div

查看:56
本文介绍了将任何纯文本替换为特定div的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将诸如[make-this-class]之类的任何纯文本替换为特定div <div class='this-div-only'></div>之类的<div class='make-this-class'></div>之类.

Trying to replace any plain-text like [make-this-class] to class like <div class='make-this-class'></div> of specific div <div class='this-div-only'></div>.

HTML:

<div class='not-others-div'>
text text [make-this-class] text
</div>
Text text text
<div class='this-div-only'> <!-- this div only -->
text text [make-this-class] text
</div>

如何通过jquery将任何纯文本[any-text]替换为dom元素的特定div类?

How to replace any plain-text [any-text] to a class of specific div of dom elements by jquery ?

推荐答案

尝试将.html()String.prototype.replace()RegExp /\[(.*)\]/

$(".this-div-only").html(function(i, html) {
  return html.replace(/\[(.*)\]/, $("<div />", {"class":"$1"})[0].outerHTML)
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class='not-others-div'>
text text [make-this-class] text
</div>
Text text text
<div class='this-div-only'> <!-- this div only -->
text text [make-this-class] text
</div>

这篇关于将任何纯文本替换为特定div的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆