Javascript jQuery.text()和.innerHTML在IE中不起作用
我试图在一个div中添加一些文本,非常简单,但我无法在IE中使用它。它在Chrome、Safari和Firefox中都可以使用 以下是jQuery:Javascript jQuery.text()和.innerHTML在IE中不起作用,javascript,jquery,html,Javascript,Jquery,Html,我试图在一个div中添加一些文本,非常简单,但我无法在IE中使用它。它在Chrome、Safari和Firefox中都可以使用 以下是jQuery: $("div#dateRanges #startDate").text(typeValues.dateRanges[1][0]); $("div#dateRanges #endDate").text(typeValues.dateRanges[1][1]); 下面是我正在编辑的html: <div id="dateRanges">
$("div#dateRanges #startDate").text(typeValues.dateRanges[1][0]);
$("div#dateRanges #endDate").text(typeValues.dateRanges[1][1]);
下面是我正在编辑的html:
<div id="dateRanges">
<span id="startDate"></span>
<span id="endDate"></span>
</div>
为了消除潜在的混乱,我遇到的问题是设置跨距的文本。另外,在页面加载后通过AJAX更新此数据的信息也很重要。第二,您需要获取原始DOM元素。您可以使用
[0]
或.get(0)
可能,text
可以工作,但您希望它被视为HTML。我想您可以在这里使用html
:
$("div#dateRanges #startDate").html(typeValues.dateRanges[1][0]);
$("div#dateRanges #endDate").html(typeValues.dateRanges[1][1]);
尝试以下代码,并确保
typeValues.dateRanges
不为空
var startDate = $("div#dateRanges #startDate");
startDate.html(typeValues.dateRanges[1][0]); //Sets the text date values
var endDate = $("div#dateRanges #endDate");
endDate.html(typeValues.dateRanges[1][1]); //enter code here
显然,这是因为jQuery的
$('#foo')
方法返回一个对象而不是一个元素。这里有一篇关于它的精彩短文:
为了解决这个问题,我可以想象,您可以使用那篇文章中建议的方法,但我的做法是只使用jQuery的
.html()
方法,不带任何参数,正如本文所建议的那样。var startDate=$(“div#dateRanges#startDate”)[0];给我一个未定义的值,这意味着没有元素匹配该选择器。如果您想匹配元素而不考虑其祖先,可以尝试使用$(“#startDate”)
;您的问题似乎在别处。jQuery保证在所有常见浏览器中都能工作,因此您的第一个代码示例应该可以正常工作。(或者你想改为html()
)你确定typeValues.dateRanges[1][0]
包含你认为它的功能吗?试着使用IE8中的开发者工具逐步浏览你的代码。我经常通过这种方式在IE上找到更好的调试信息。类型值是“月-年”格式的日期字符串,我也这么认为,这就是为什么发生这种情况时我感到惊讶…@Joshkunz:startDate.html(“Hai”)->如果Hai将重新使用span的html,那么typeValues.dateRanges.Pls将出现问题
$("div#dateRanges #startDate").html(typeValues.dateRanges[1][0]);
$("div#dateRanges #endDate").html(typeValues.dateRanges[1][1]);
var startDate = $("div#dateRanges #startDate");
startDate.html(typeValues.dateRanges[1][0]); //Sets the text date values
var endDate = $("div#dateRanges #endDate");
endDate.html(typeValues.dateRanges[1][1]); //enter code here