之前我在jQuery和Javascript中做过一些事情,但不幸的是我不是专家。我找不到任何有关如何使用尽可能少的资源完成任务的提示。你们可以帮助我:
以下是我想要做的事情:
我想在页面上找到(使用正则表达式)所有类似BB代码的元素,如下所示:
[ndex here = parameter randomdata]
我想用我看到的ajax-call中收到的内容替换它们中的每一个:
call.php?ndex = here =参数randomdata
或我在[ndex] -tag中提取的任何参数。
到目前为止,这是我的解决方案/思考过程:
$(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
}
});
}
});
});
我真的不知道如何在这里工作。我的素描看起来很笨重而复杂。必须有一个更优雅和直接的解决方案。
非常感谢你们的帮助。 :)
答案 0 :(得分:1)
我的建议是将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才能考虑到这一点,但是你节省了大量的服务器调用时间
答案 1 :(得分:1)
我会做这样的事情:
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);
});
});
});
我被节点而不是jquery取代,因为我觉得使用jquery处理textNode相当糟糕。如果你不在乎并且宁愿做野蛮人的方式,你可以简单地替换所有第一部分:
$(function(){
// Get the elements we want to scan ( being sharper would be better )
$('body').each(function(){
// With no " in argument of [ndex ...]
$(this).html( $(this).html().replace(/\[ndex\s+([^"]*?)\]/mg,'<span class="ndex-to-replace" title="$1"></span>') );
// With no ' in argument of [ndex ...]
//$(this).html( $(this).html().replace(/\[ndex\s+([^']*?)\]/mg,'<span class="ndex-to-replace" title='$1'></span>') );
});
// Make the ajax calls
/* ... */
});