代码浏览器兼容性问题JavaScript / Jquery

时间:2014-04-23 05:53:53

标签: javascript jquery sharepoint

以下代码适用于Chrome和Firefox,但是onbuttonclick事件在资源管理器中不起作用,XML数据提取工作正常,但btn1上的onClick事件运行不正常。 Followinf是代码,目的是使用自动完成jquery功能我们正在填充输入框(水果),点击按钮我会在文本区域显示相应的代码细节。(我在sharepoint 2010工作) 我觉得有一些问题(在函数GetCode中)     var index=arrt.indexOf( $("#fruits" ).val());

IE中的

部分未执行

<!-- Load jQuery, jQuery UI and jQuery ui styles from jQuery website -->
<link   href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script><script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<!-- Source our javascript file with the jQUERY code -->
<script>
/*  jQuery ready function. Specify a function to execute when the DOM is fully loaded.  */
var arr = [];
var arrt = [];
var source = [];
if (window.XMLHttpRequest)
{

 xhttp=new XMLHttpRequest();
}
else // code for IE5 and IE6
{

  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xhttp.open("GET","Data.xml",false);
xhttp.send();
alert("done");
xmlDoc=xhttp.responseXML;


 var i=0;
for (i=0;i<xmlDoc.getElementsByTagName("Field_3").length;i++)
{  
  var tmp;
   tmp=xmlDoc.getElementsByTagName("Field_3")[i].childNodes[0].nodeValue;
  // alert(tmp);
   arr.push(tmp);
 }

var j=0;
while(j<=i-1)
{ 
var tmp;
try
{
tmp=xmlDoc.getElementsByTagName("Field_2")[j].childNodes[0].nodeValue;
source.push(tmp);
}
catch(err)
{
 source.push("NA");
}
j=j+1;
}
j=0;
while(j<=i-1)
{ 
 arrt.push(xmlDoc.getElementsByTagName("Field_0")[j].getAttribute("Field_0"));
j=j+1;
}

var yv=" ";
var st=" "; 
document.getElementById("Code").value=yv;
document.getElementById("fruits").value=st;</script><script>

 function myFunction()
 {
      $( "#fruits" ).autocomplete({
  /*Source refers to the list of fruits that are available in the auto complete list. */
  source:arrt,
  /* auto focus true means, the first item in the auto complete list is selected by default. therefore when the user hits enter,
  it will be loaded in the textbox */
  autoFocus: true ,

 });
}
function GetCode()
{
  var index=arrt.indexOf( $("#fruits" ).val());
  yv=arr[index];
  if(yv=="NA" || yv=="Direct Variable" )
  {
   var str="";
   if(source[index]!="NA")
    str = "select "+ arrt[index]+" from "+ source[index];
   else
    str= "!!!!!!No Data Source is Available!!!!";

      document.getElementById("Code").value=str;
  }

  else
  document.getElementById("Code").value=yv;
  //alert(yv);
}
document.getElementById("Code").value=yv;</script>
<h1>Auto complete Metrics to Source</h1>
<!-- textbox and label that are attached with the autocomplete -->
<label for="fruits">Select Matric : <input name="fruits" id="fruits" onkeypress="myFunction()" type="text"/></label>
<button name="btn1" id="btn1" onclick="GetCode()" type="button">Get Code</button>
<br/><br/><textarea id="Code" rows="6" cols="60"></textarea>

2 个答案:

答案 0 :(得分:0)

你可以删除额外的&#34;,&#34; autoFocus之后:从函数myFunction中为true。

这会导致IE出错,请重试。

 function myFunction()
 {
    $( "#fruits" ).autocomplete({
    /*Source refers to the list of fruits that are available in the auto complete list. */
    source:arrt,
    /* auto focus true means, the first item in the auto complete list is selected by 
    default.therefore when the user hits enter,
    it will be loaded in the textbox */
    autoFocus: true
 });

答案 1 :(得分:0)

整个问题在于数组上的indexOf函数,我编写了自己的indexOf函数,它运行良好,没有任何问题,在这里我正在编写我的代码: -

function getIndex(var str)
{
var arrayLength = arrt.length;
    var i;
for (i = 0; i < arrayLength; i++) 
{
        if(str==arrt[i])
             break;
}
 return i;
}