[英]IE8 and javascript object property, text
Using .text
object property in JavaScript can produce unexpected results in IE8 (blank string or undefined even when it contains a value).在 JavaScript 中使用
.text
object 属性可能会在 IE8 中产生意外结果(空白字符串或未定义,即使它包含一个值)。
You can use .innerHTML
object property as a workaround.您可以使用
.innerHTML
object 属性作为解决方法。 IE8 seems to like this. IE8似乎喜欢这个。
Or, for reliable cross-browser compatability you can use jquery instead to access an object's text property and I could have do so with: $(this).text()
See the answer for a good jQuery solution.或者,为了获得可靠的跨浏览器兼容性,您可以使用 jquery 来访问对象的文本属性,我可以这样做:
$(this).text()
查看答案以获得良好的 jQuery 解决方案。
Original Question: Is, 'text' a reserved javascript word in IE8?原始问题: “文本”是 IE8 中保留的 javascript 字吗? I am curious because I could not find any resource that states this.
我很好奇,因为我找不到任何说明这一点的资源。
* JavaScript ( optionObj.innerHTML
) Solution: * * JavaScript (
optionObj.innerHTML
) 解决方案:*
// Look for a match in the section dropdown and select it.
$.each($('#' + mySelect + ' option'), function(key, optionObj) {
// Switched 'optionObj.text' to 'optionObj.innerHTML' for cross-browser compatibility
if (optionObj.innerHTML == strTextToMatch) {
// Found a match
$('#' + mySelect).val(optionObj.text);
$('#' + mySelect).trigger('change');
}
});
In IE8, optionObj.text
would sometimes return a blank string, even when I could see in my debugger that it contained a value (I could see by expanding the optionObj
object), What was strange is that 10% of the time, optionObj.text
would return the actual value other than a blank string.在 IE8 中,
optionObj.text
有时会返回一个空白字符串,即使我可以在调试器中看到它包含一个值(我可以通过扩展optionObj
对象看到),但奇怪的是 10% 的时间是optionObj.text
将返回除空白字符串之外的实际值。 That being said, optionObj.innerHTML
seems to work reliably.话虽如此,
optionObj.innerHTML
似乎工作可靠。
No, text is neither a reserved word, nor a future reserved word, in JScript.不,文本在 JScript 中既不是保留字,也不是将来的保留字。
http://msdn.microsoft.com/en-us/library/0779sbks(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/0779sbks(v=vs.85).aspx
Edit OH, I see it now.编辑哦,我现在看到了。
You're using the wrong each
function — $.each()
instead of .each()
.您使用了错误的
each
function — $.each()
而不是.each()
。
You're also using .text
instead of .text()
.您还使用
.text
而不是.text()
。 Fixes:修复:
var $mySelect = $('#' + mySelect);
$mySelect.find('option').each(function()
{
var $option = $(this),
text = $option.text();
if (text == strTextToMatch) {
// Found a match
$mySelect.val(text);
$mySelect.trigger('change');
}
});
but guess what: that's is totally unnecessary.但你猜怎么着:那是完全没有必要的。 This will suffice:
这就足够了:
var $mySelect = $('#' + mySelect),
newVal = $mySelect.find('option:contains(' + strTextToMatch + ')').val();
$mySelect.val(newVal).change();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.