在有人说我没有读过之前,我可能会说我几乎读到了与我的问题相关的所有内容.但我找不到答案.所以,我有一个简单的AJAX脚本,可以将我的外部文件加载到预定义的div中.这是那些脚本的代码:
function loadTwitter()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your Browser Don't Support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.getElementById("column_twitter").innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET","../includes/home/twitter.php",true);
xmlHttp.send(null);
}
Run Code Online (Sandbox Code Playgroud)
它在我测试的每个浏览器(FF,Opera,Chrome,Safari)中运行良好,但在IE7内部不想将我的外部php文件注入预定义的div.它总是保持我在div里面的默认文本......我认为问题出在这一行:
document.getElementById("column_twitter").innerHTML=xmlHttp.responseText;
Run Code Online (Sandbox Code Playgroud)
所以,有任何建议如何解决这个IE(7及以上)?
这是相关的HTML:
<div id="navcontainer">
<ul id="navlist">
<li><a href="#tab1">Item one</a></li>
<li><a href="#tab2">Item two</a></li>
<li><a href="#tab3">Item three</a></li>
<li><a href="#tab4">Item four</a></li>
<li><a href="#tab5">Item five</a></li>
</ul>
</div>
Run Code Online (Sandbox Code Playgroud)
vertical.js的内容
function tabber() {
var li = document.getElementById("navcontainer");
var as = document.getElementById('navlist');
return;
}
window.onload = tabber();
Run Code Online (Sandbox Code Playgroud)
执行tabber()函数时,对document.getElementById的函数调用返回null.为什么?元素navcontainer肯定存在.有线索吗?
我花了一个多小时试图解决这个问题,我很绝望.我试图访问从ASP.NET控件创建的DOM中的节点.我正在使用完全相同的ID,我可以看到它们在页面呈现后查看HTML源代码时匹配.这是我的[MODIFIED根据建议,但仍然没有工作]代码:
ASP.NET标头
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
$(document).ready(
var el = document.getElementById('<%= txtBox.ClientID %>');
el.onchange = alert('test!!');
)
</script>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
ASP.NET Body
<asp:TextBox ID="txtBox" runat="server"></asp:TextBox>
Run Code Online (Sandbox Code Playgroud)
从上面得到的Javascript和HTML
<script type="text/javascript">
$(document).ready(
var el = document.getElementById('MainContent_txtBox');
el.onchange = alert('test!!');
)
</script>
...
<textarea name="ctl00$MainContent$txtBox" id="MainContent_txtBox"></textarea>
Run Code Online (Sandbox Code Playgroud)
我只能假设脚本在控件ID解析之前加载,但是当我使用Chrome的"Inspect Element"功能查看时间轴时,似乎并非如此.当我创建一个常规的textarea框来测试和实现相同的代码(当然是不同的id)时,会触发警告框.
我到底在这里错过了什么?这让我发疯了.<
编辑:有效的Wierd代码,但仅限于初始页面加载; 点燃onload而不是onchange.甚至jQuery说.ready已经显然无法正常工作.啊!!
$(document).ready(function() {
document.getElementById('<%= txtBox.ClientID %>').onchange = alert('WORKING!');
})
Run Code Online (Sandbox Code Playgroud) 我无法将.innerHtml替换为嵌套在其他div标签中的div标签.我用firebug来确保getElementById()正常工作.
<div class="a">
<div class="b"><a href="myUrl"><div id="hello"></div></a></div>
</div>
Run Code Online (Sandbox Code Playgroud)
在我使用的javascript代码中:
document.getElementById("hello").innerHTML = "hello world";
Run Code Online (Sandbox Code Playgroud)
使用firebug,我已经验证了"hello"div被拾取(也就是说,它不是null).但字符串"你好世界"没有出现.请帮助.谢谢.
我有一个下拉菜单,我想要的是当下拉时<div>显示以前隐藏的显示.但我不断收到错误:
uncaught typerror:object #<HTML Document> has no method 'getElementByID'
Run Code Online (Sandbox Code Playgroud)
这是代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>landing3</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function showdiv() {
document.getElementByID("DIV1").style.display = "inline-block";
}
</script>
</head>
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<select id="awf_field-28500717" name="custom Country" tabindex="503" onChange="showdiv()">
<option class="multiChoice" value="United States">United States</option>
<option class="multiChoice" value="Canada">Canada</option>
<option class="multiChoice" value="United Kingdom">United Kingdom</option>
<option class="multiChoice" value="Australia">Australia</option>
<option selected>Select</option>
</select>
<div id="DIV1" name="DIV1" style="display:none;">
Test
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) getElementById()正在返回对表单上检索"隐藏"字段的调用null.
我已经研究了其他SO问题,许多人在调用中使用隐藏字段的'name'而不是'id',getElementById()或者getElementById()在包含隐藏字段的实际html处理之前在文件顶部的脚本中执行浏览器.这两个错误都可以解释为什么getElementById()要返回null它们.
我可能错了,但我不认为这是我的情况.
这是我的表格:
<form name="theDeleteItemForm" id="deleteItemForm" action="deleteTheSelectedItem"
method="post"
<input type="hidden" id="theHiddenField" name="deleteThisSelectedItem" value="">
</form>
Run Code Online (Sandbox Code Playgroud)
这里有一个Javscript处理程序,当点击页面上的"删除"按钮时,它成功执行(我可以告诉我alert(),因为我在下面的处理函数中弹出了框):
function deleteItem()
{
alert("Just entered deleteItem()");
var theFieldToDelete = document.getElementById('theHiddenField');
// THIS IS THE PLACE WHERE I FOUND THAT 'theFieldToDelete' WAS 'null'
alert("Just got the hidden field element, which is: " + theFieldToDelete );
// THIS DOES NOTHING MORE THAN TO PREVENT THE 'alert' THAT FOLLOWS FROM APPEARING
theFieldToDelete.value …Run Code Online (Sandbox Code Playgroud) 我最近在我的网站上的几个页面添加了以下Javascript代码行,以便从CSS类别"Category-H1"中提取H1元素的标题标记.
document.title = document.getElementsByClassName("Category-H1")[0].innerHTML;
Run Code Online (Sandbox Code Playgroud)
我很好奇,我可以做一些类似的事情,使用我的H2元素中的JS拉入描述标签吗?
我在我的Web应用程序中创建这些代码
<script type="text/javascript">
document.getElementById('articleTitle').value = "Ehsan" ;
</script>`
Run Code Online (Sandbox Code Playgroud)
但结果我的输入没有价值
我的意见是:
<input id="articleTitle" name="articleTitle" class="input-xxlarge"
type="text" placeholder="title">
Run Code Online (Sandbox Code Playgroud)
我的javascript代码高于html代码
我正在尝试计算显示的许多输入的总和,以便制作发票.所有必须开发票的产品都是我的数据库中的记录器,我编写了这个JavaScript函数来计算总数:
<script type="text/javascript">
function getItems()
{
var items = new Array();
var itemCount = document.getElementsByClassName("items");
var total = 0;
for(var i = 0; i < itemCount.length; i++)
{
total = total + document.getElementById("p"+(i+1)).value;
}
return total;
document.getElementById('tot').value= total;
}
getItems()</script>
Run Code Online (Sandbox Code Playgroud)
问题是我Uncaught TypeError: Cannot read property 'value' of null 上线了 total = total + document.getElementById("p"+(i+1)).value;
我真的不明白为什么,因为我的所有变量都被声明了.
如何将此脚本与多个ID一起使用?不适用于我尝试添加的第二个ID
<script type="text/javascript">
window.onload = (function () {
var elem = document.getElementById("id1","id2").onclick = function()
{
fbq('track', 'InitiateCheckout');
}
});
</script>
Run Code Online (Sandbox Code Playgroud)