jQuery/Javascript:在HTML文档中进行更复杂的搜索和替换
我以前用jQuery和Javascript做过一些工作,但不幸的是我不是专家。我找不到任何关于如何使用尽可能少的资源来完成任务的提示。你们可以帮我解决这个问题: 以下是我想做的:jQuery/Javascript:在HTML文档中进行更复杂的搜索和替换,javascript,jquery,ajax,regex,replace,Javascript,Jquery,Ajax,Regex,Replace,我以前用jQuery和Javascript做过一些工作,但不幸的是我不是专家。我找不到任何关于如何使用尽可能少的资源来完成任务的提示。你们可以帮我解决这个问题: 以下是我想做的: $(document).ready(function() { var pattern = /\[ndex\s+(.*?)\]/mg; var documentText = $(document.body).text(); var matches = documentText.match(patt
$(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
}
});
}
});
});
我想在页面上查找(使用正则表达式)所有类似BB代码的元素,这些元素的外观如下:
[ndex此处=参数随机数据]
然后,我想用从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调用。首先进行搜索,然后在另一轮上用相应的数据替换每个对象
$(document).ready(function() {
var pattern = /\[ndex\s+(.*?)\]/mg;
var documentText = $(document.body).text();
var matches = documentText.match(pattern);
$.ajax({
url:'call.php',
method:'POST',
data: matches,
success: function(data){
//replace every matched element with the corresponding data
});
});
不过,您必须修改call.php以考虑到这一点,但是您将节省大量对服务器的调用,因此节省时间我会这样做:
function ndex_treat(n) {
// If element is ELEMENT_NODE
if(n.nodeType==1)
{
// If element node has child, we pass them to function ndex_treat
if(n.hasChildNodes())
for(var i= 0; i<n.childNodes.length; i++)
ndex_treat(n.childNodes[i]);
}
// If element is TEXT_NODE we replace [ndex ...]
else if(n.nodeType==3)
{
var matches, elemNdex, elemText;
// While there is one
while(/\[ndex\s+(.*?)\]/m.test(n.nodeValue))
{
// Taking what's before (matches[1]), the "attribute" (matches[2]) and what's after (matches[3])
matches= n.nodeValue.match(/^([\s\S]*?)\[ndex\s+(.*?)\]([\s\S]*)$/m)
// Creating a node <span class="ndex-to-replace" title="..."></span> and inserting it before current text node element
elemNdex= document.createElement("span");
elemNdex.className= 'ndex-to-replace';
elemNdex.title= matches[2];
n.parentNode.insertBefore(elemNdex, n);
// If there was text before [ndex ...] we add it as a node before
if(matches[1]!=="")
{
elemText = document.createTextNode(matches[1]);
elemNdex.parentNode.insertBefore(elemText, elemNdex);
}
// We replace content of current node with what was after [ndex ...]
n.nodeValue=matches[3];
}
}
}
$(function(){
// Get the elements we want to scan ( being sharper would be better )
$('body').each(function(){
// Passing them to function ndex_treat
ndex_treat(this);
});
// Make the ajax calls
$('.ndex-to-replace').each(function(){
// Don't know if necessary
var current= this;
$.get('call.php?ndex='+encodeURIComponent(this.title),function(data){
$(current).replaceWith(data);
});
});
});
功能无损检测(n){
//如果元素是元素\节点
if(n.nodeType==1)
{
//若元素节点有子节点,我们将其传递给函数ndex_treat
if(n.hasChildNodes())
对于(var i=0;我不确定这是否是实现所需的方法。您应该在服务器端替换文本,而不是通过ajax调用感谢您的回复。我需要进行ajax调用,因为替换的文档不一定在使用php的服务器上运行。谢谢!这太好了!我有点担心传递javascript-但是,我的实际问题是:我需要用html代码(由我的php文件生成)替换bb代码,我不确定如何用从对象派生的相应html代码替换文档中的文本(?)。有什么提示吗?你可以做data:{ndex:matches}
你可以在php页面中以$_POST['ndex']数组的形式访问它,用dom节点或innerHTML(或等效的jquery html())替换。谢谢这么多人,你太棒了!:)非常感谢。你已经给出了一个使用复制+粘贴的答案。太棒了!:)不过,我仍将尝试使用Pablo建议的解决方案。在运行该解决方案之前,我将使用您的解决方案。您帮助我更好地理解了Javascript中搜索/替换的工作方式,谢谢您的帮助!