【发布时间】:2012-07-01 19:20:12
【问题描述】:
我以前在 jQuery 和 Javascript 方面做过一些事情,但不幸的是我不是专家。我找不到任何关于如何使用尽可能少的资源来完成任务的提示。你们也许可以帮帮我:
这是我想做的:
我想找到(使用正则表达式)页面上所有类似 BB 代码的元素,如下所示:
[此处的索引=参数随机数据]
然后我想用我从 ajax 调用收到的内容替换它们,如下所示:
call.php?ndex=here=参数随机数据
或我从相应的 [ndex] 标记中获取的任何参数。
到目前为止,这是我的解决方案/思考过程:
$(document).ready(function() {
var pattern = /\[ndex\s+(.*?)\]/mg;
var documentText = $(document.body).text();
var matches = documentText.match(pattern);
$('*').each(function () {
var searchText = this;
if ($(searchText).children().length == 0) {
$.each(matches, function() {
//here is where I would need to check for a match and make a call
}
});
}
});
});
我真的不知道如何从这里开始工作。我的草图看起来非常笨重和复杂。必须有一个更优雅、更直接的解决方案。
非常感谢你们的帮助。 :)
【问题讨论】:
-
我不确定这是实现您想要的方式。您应该替换文本服务器端,而不是通过 ajax 调用
-
感谢您的回复。我需要进行 ajax 调用,因为有替换的文档不一定在带有 php 的服务器上运行。
标签: javascript jquery ajax regex replace