简体   繁体   English

AJAX IE找不到元素(getElementByID())

[英]AJAX IE doesn't find element ( getElementByID() )

I've searched everywhere i could but i did not find a solution to this specific problem. 我已经在所有可能的地方进行了搜索,但是没有找到解决此特定问题的方法。

To put it simple. 简单地说。 I have a page with some javascript code and a DIV. 我的页面上有一些JavaScript代码和DIV。 I use ajax to load a second page inside that DIV. 我使用ajax在该DIV中加载第二页。 The loaded page has one element which id is "someid". 加载的页面有一个id为“ someid”的元素。

someid is <input type="hidden" id="someid" name="someid" value="sdasasdadad" /> someid是<input type="hidden" id="someid" name="someid" value="sdasasdadad" />

then, on the loaded page i call a function that is defined on the "global" page that begins with these two lines: 然后,在加载的页面上,我调用在“全局”页面上定义的以以下两行开头的函数:

var=document.getElementById("someid").value;

alert(var);

works fine on firefox but not on IE. 在Firefox上工作正常,但在IE上无法正常工作。 On IE it doesnt display the alert and doesnt execute any code after those lines. 在IE上,它不显示警报,并且在这些行之后不执行任何代码。 I think the problem is that when the javascript code was evaluated, the "someid" element didnt exist yet and IE doesnt seem to understand that now it exists after i loaded a page using ajax. 我认为问题在于,当评估javascript代码时,“ someid”元素尚不存在,并且IE似乎不了解,现在我使用ajax加载页面后它就存在了。 I hope i made myself clear? 我希望我能说清楚吗?

I need to do things this way because this is only a small part of a bigger interface. 我需要这样做,因为这只是较大接口的一小部分。 I have no knowledge on jquery but it should work with this javascript code too i guess! 我对jquery不了解,但我猜它也应与此JavaScript代码一起使用! How can i make this work? 我该如何工作?

Thanks a lot in advance! 在此先多谢!

var is used to declare variables. var用于声明变量。 Do something like var el = document.getEl... 做类似var el = document.getEl...

This is assuming your code is exactly the same as your production code, otherwise please show us the real code 假设您的代码与生产代码完全相同,否则请向我们展示真实代码

EDIT: Since you said it "works" in Firefox, your code is probably inconsistent, since var = 3 in Chrome throws a SyntaxError and I imagine it would do so in Firefox too. 编辑:由于您说过它在Firefox中“有效”,因此您的代码可能不一致,因为Chrome中的var = 3会引发SyntaxError而且我想在Firefox中也可以。

The var word is a keyword in Javascript. var字是Javascript中的关键字。 Here you are trying to use a keyword as an identifier which is illegal. 在这里,您尝试使用关键字作为非法标识符。 Using a name other than var like myVar will fix the problem. 使用除var其他名称(如myVar可以解决此问题。

var myVar =document.getElementById("someid").value;
alert(myVar);

Try it like that. 这样尝试。 var is a reserved keyword for javascript var是javascript的保留关键字

var someVar = document.getElementById("someid").value;

alert(someVar);

I would try debugging this by alerting the element and then the value : 我会尝试通过提示元素然后值来调试它:

var element = document.getElementById("someid");

alert(element);

var valueOfElement = element.value;

alert(valueOfElement);

I'm sure you haven't but you can't use var as a variable name, it's a Javascript keyword. 我确定您还没有,但是您不能使用var作为变量名,它是一个Javascript关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM