Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery在多个html文件中搜索字符串_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用jquery在多个html文件中搜索字符串

Javascript 使用jquery在多个html文件中搜索字符串,javascript,jquery,html,Javascript,Jquery,Html,我会尽量用几句话来解释我的问题。 我有一个带有各种iFrame的HTML。在一个iframe中有一个目录(TOC),而在另一个iframe中,有TOC中突出显示的相应元素的内容。因为有各种各样的TOC,点击一个链接,我们可能会被带到一个属于另一个TOC的主题,因此我希望TOC框架能被正确的TOC重新加载。为此,由于每个主题在TOC中都有一个唯一的id,因此我在所有TOC中搜索主框架中加载的主题id,当我找到想要的TOC时,我将其加载到TOC框架中 到目前为止,我编写的代码是Segunt: /*f

我会尽量用几句话来解释我的问题。 我有一个带有各种iFrame的HTML。在一个iframe中有一个目录(TOC),而在另一个iframe中,有TOC中突出显示的相应元素的内容。因为有各种各样的TOC,点击一个链接,我们可能会被带到一个属于另一个TOC的主题,因此我希望TOC框架能被正确的TOC重新加载。为此,由于每个主题在TOC中都有一个唯一的id,因此我在所有TOC中搜索主框架中加载的主题id,当我找到想要的TOC时,我将其加载到TOC框架中

到目前为止,我编写的代码是Segunt:

/*function called on load of each topic - it gets the topic unique id as parameter*/
function highlight(id) {
  /*the names of the HTML files containing the different tocs*/
  var tocs = ["toc.htm", "toc_documentazione.htm", "toc_flussiesteri.htm", "toc_garante.htm", "toc_legittimita.htm", "toc_normativa.htm", "toc_settori.htm", "toc_sicurezza.htm", "toc_sistemadp.htm", "toc_vistaarticolato.htm"]
  var i = 0;
  /*search within the different TOCs until you find a correspondence or there are no more TOCs*/
  while (!changeTOC(tocs[i], "a" + id) && i < tocs.length) {
    i = i + 1;
  }

  /*this line is probably wrong but the idea is to load the found TOC in the appropriate frame*/
  $(content).load(tocs[i - 1] + " #content");

}


/*function using ajax to search the id into the HTML file passed as parameter (newToc) returning the search outcome*/
function changeTOC(newToc, id) {

  var found = false;
  $.get(newToc, "html").done(
    function(temp_toc) {
      /*if the HTML contains the id we search for we return true*/
      if (temp_toc.indexOf(id) != -1)
        found = true;
    });

  /*else we return false*/
  return found;
} 
/*函数在加载每个主题时调用-它获取主题唯一id作为参数*/
功能突出显示(id){
/*包含不同TOC的HTML文件的名称*/
变量tocs=[“toc.htm”、“toc_documentazione.htm”、“toc_flussiesteri.htm”、“toc_garante.htm”、“toc_legittimita.htm”、“toc_normativa.htm”、“toc_settori.htm”、“toc_sicurezza.htm”、“toc_sistemadp.htm”、“toc_vistaarticolato.htm”]
var i=0;
/*在不同的TOC中搜索,直到找到对应项或没有更多的TOC*/
而(!changeTOC(tocs[i],“a”+id)和&i
我遇到的问题是,我使用while循环搜索各种TOC文件。我做了一些调试,不管包含我正在搜索的id的TOC位于第一个位置,while执行了10个周期,直到最后它才告诉我它找到了匹配的TOC,它确实是列表中的第一个

希望我能说清楚。
感谢您的帮助

我终于通过使用syncronus设置为true的ajax调用实现了这一点。我不会在这里发布所有的代码,因为这会让人困惑,但下面是我与上面编写的代码相比所做的更改,现在一切都很好。也许它的性能不是最优的,但我没有这个顾虑,所以我很满意:) 希望这能帮助别人

/*function called on load of each topic - it gets the topic unique id as parameter*/

function highlight(id) {
var tmpVal = sessionStorage.getItem('key');
//we check if there is another element in the TOC currently highlighted in bold, if so we remove the highlight
if(tmpVal) {
    var tmpEle=parent.window.frames[0].document.getElementById('a'+tmpVal);
    if (tmpEle) {
        tmpEle.className='';
    }
}

//loop through all TOCs to find the one containing the selected topic
var tocs = ["toc.htm","toc_documentazione.htm","toc_flussiesteri.htm","toc_garante.htm","toc_legittimita.htm","toc_normativa.htm","toc_settori.htm","toc_sicurezza.htm","toc_sistemadp.htm","toc_vistaarticolato.htm"];     
var i=0;
while (!changeTOC(tocs[i],"a"+id)&&i<tocs.length){
    i=i+1;
}

//get currently loaded TOC
var currentToc=$("#toc_iframe",parent.document).attr("src");
var indexCurrentTOC=tocs.indexOf(currentToc);
//we check if the matching TOC is the current one, if so we don't change anything
if(!changeTOC(tocs[indexCurrentTOC],"a"+id)){
    $("#toc_iframe",parent.document).attr("src",tocs[i]);
}
var myElt=parent.window.frames[0].document.getElementById('a'+id);
//highlight current element in the TOC
myElt.focus();
myElt.className+=' active';
scrollTo(myElt.offsetLeft-48, myElt.offsetTop-(parent.document.body.clientHeight/3));
sessionStorage.setItem("key", id);
}

//searches for the element with given id into the toc file newToc 
function changeTOC(newToc,id){

var found = false;
$.ajax({
    url: newToc,
    async: false,
    context: document.body
    }).done(function(temp_toc) {
                if(temp_toc.indexOf(id)!=-1){
                    found = true;
                }
    });
return found;
/*函数在加载每个主题时调用-它获取主题唯一id作为参数*/
功能突出显示(id){
var tmpVal=sessionStorage.getItem('key');
//我们检查TOC中是否有另一个元素当前以粗体突出显示,如果是,则删除突出显示
如果(tmpVal){
var tmpEle=parent.window.frames[0].document.getElementById('a'+tmpVal);
如果(tmpEle){
tmpEle.className='';
}
}
//循环遍历所有TOC以查找包含所选主题的TOC
变量tocs=[“toc.htm”、“toc_documentazione.htm”、“toc_flussiesteri.htm”、“toc_garante.htm”、“toc_legittimita.htm”、“toc_normativa.htm”、“toc_settori.htm”、“toc_sicurezza.htm”、“toc_sistemadp.htm”、“toc_vistaarticolato.htm];
var i=0;

当(!changeTOC(tocs[i],“a”+id)和&iYou试图从执行异步操作的函数内部返回值时,会出现基本错误(即
changeTOC
调用
$.get
)。使用回调或jQuery承诺。你不能像那样在while循环中处理这些多个get请求。看看这个问题,你真的需要找到另一种方法将iFrame页面连接到TOC。一次加载它们,只是为了找到链接,似乎效率不高。谢谢@TrueBlueAussie。我没有太多选择因为TOC文件是自动生成的。这就是为什么我大量使用javascript和jquery来定制整体行为的原因。我对javascript不是很精通,这就是为什么我很挣扎,但我也会看看回调和承诺