在包含流的html上用javascript动态替换str
Dynamic str replace with javascript on html containing stream
我有一个html,它看起来大致像这个
<div id="wallPostTemplate">
<li id='comment-content--oOo-id-oOo-' class='comment-content' comment_id='-oOo-id-oOo-'>
<a class='comment-avatar' href='-oOo-link-oOo-'>
<img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>-oOo-korisnikPodaci.Slika-oOo-'></a>
-oOo-text-oOo-
</li>
</div>
现在,我用$('#wallPostTemplate').text()
将html的内容放入一个字符串中,然后我想动态替换字符串-oOo-id-oOo-
的出现无论我在变量中存储了什么数据,比如-oOo-id-oOo-
、-oOo-text-oOo-
等等…
现在我尝试了很多方法来解决这个问题,但最终总是没有解决方案。这是我最近的一次尝试。请帮助Q_Q
var regex = new RegExp('{'-oOo'-'+index+''-oOo'-}', "g");
post = post.replace(regex, value);
我也试过str.replace
,但它不起作用。。。似乎新的线路是个问题,但我不知道如何克服这个。。。。。
编辑:
为了更容易理解,我在jsfidle上做了一个例子:http://jsfiddle.net/DdBFB/正如你所看到的,你什么也看不见。。。。当您通过html函数获取内容时,它是不起作用的。
如果我理解你的问题,你的目标是制作一个类似于post模板的东西,它将在运行时/客户端加载。因此,如果这真的是你的目标,你可以尝试使用jQuery模板插件
您案例的样本:(尚未测试)
var tmlpPost = "<div id='${postId}'>" +
" <li id='comment-content-${postId}' class='comment-content' comment_id='${postId}'>" +
" <a class='comment-avatar' href='${link}'>" +
" img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>${korisnikPodaciSlika}'>" +
" </a> " +
" ${postText}" +
" </li>" +
"</div>";
$.tmpl( tmlpPost, { "postId" : "123" , "link" : "http://posturl.com", "korisnikPodaciSlika" : "something", "postText" : "Post text goes here..."}).appendTo( "#yourPostWall" );
或者使用<script>
而不是var
:请参阅此Fiddle
<script id="tmlpPost" type="text/x-jquery-tmpl">
<div id='${postId}'>
<li id='comment-content-${postId}' class='comment-content' comment_id='${postId}'>
<a class='comment-avatar' href='${link}'>
<img class='small_avatar' src='<?="../" . MESTO_ZA_UPLOAD_FAJLOVA_KORISNIKA ?>${korisnikPodaciSlika}'>
</a>
${postText}
</li>
</div>
</script>
现在页面加载:
$("#tmlpPost").tmpl( { "postId" : "123" , "link" : "http://posturl.com", "korisnikPodaciSlika" : "something", "postText" : "Post text goes here..."}).appendTo( "#output" );
虽然您的问题不太清楚,但我已经给出了一个替换基于正则表达式的值的示例。
var source = "-oOo-korisnikPodaci.Slika-oOo-";
var id = "korisnikPodaci.Slika";
var regex = new RegExp(''-oOo'-' + id + ''-oOo'-', "gmi");
// Match
source.replace(regex, "hello world"); // => "Hello World"
// Mismatch
source = "-oO!!!o-korisnikPodaci.Slika-oO!!!o-";
source.replace(regex, "hello world"); // => "-oO!!!o-korisnikPodaci.Slika-oO!!!o-"
好吧,我认为答案是我把regex标志设置错了,所以我去了jsfiddle并摆弄了一下,你可以在那里看到答案:)
http://jsfiddle.net/DdBFB/11/
我错过了多行标志Q_Q。。。。我不擅长regex,即使在php中也不行,更不用说js了。。。
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 用DRY方式Javascript/JQuery动态替换HTML
- 在包含流的html上用javascript动态替换str
- 用按钮动态替换java脚本文件
- 如何使用jQuery动态替换类的一部分
- 动态替换下拉菜单选项jQuery
- 用我的类动态替换css类
- 如何使用 javascript 中的替换函数动态替换图片 src
- JavaScript/PHP 从数据库动态替换图像源
- 使用 Javascript 将 HTML 标记动态替换为更复杂的代码
- 动态替换广告代码
- 如何动态替换HTML5视频源
- 动态替换 href
- 简单的Javascript输入表单,具有动态替换的必需字段消息DOM
- 如何动态替换onClick事件
- 使用jQuery用隐藏字段动态替换复选框
- 使用javascript将一个字符动态替换为另一个字符,而不将页面返回到初始状态
- 如何动态替换jQuery MOBILE中的过滤器搜索栏?
- 在Javascript中动态替换JSON属性
- 是否可以动态替换html页面的脚本部分