做getElementsByClassName
(等类似的功能getElementsByTagName
和querySelectorAll
)的工作方式相同getElementById
,还是他们返回元素的数组?
我问的原因是因为我试图改变所有元素的样式getElementsByClassName
.见下文.
//doesn't work
document.getElementsByClassName('myElement').style.size = '100px';
//works
document.getElementById('myIdElement').style.size = '100px';
Run Code Online (Sandbox Code Playgroud) 码:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript">
$("#clicker").click(function () {
alert("Hello!");
$(".hide_div").hide();
});
</script>
Run Code Online (Sandbox Code Playgroud)
上面的代码不起作用.当我点击#clicker时,它不会发出警报,也不会隐藏.我检查了控制台,没有错误.我还检查了JQuery是否正在加载,确实是.所以不确定问题是什么.我还做了一个带有警报的文件就绪功能,这样做不能确定我做错了什么.请帮忙.谢谢!
text
如果可能的话,我想在使用JavaScript 的类型的输入字段中禁用写入.输入字段从数据库填充; 这就是为什么我不希望用户修改其值.
我正在尝试使用getElementById()获取元素,但即使元素存在,它也会返回null.我究竟做错了什么?
<html>
<head>
<title>blah</title>
<script type="text/javascript">
alert(document.getElementById("abc"));
</script>
</head>
<body>
<div id="abc">
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我正在使用Bootstrap 3.0.2和Bootstrap-select插件.
这是我的选择列表:
<select class="selectpicker" data-live-search="true" data-size="7">
<option>Petr Karel</option>
<option>Honza Novák</option>
<option>David Egydy</option>
<option>Sláva Ková?</option>
<option>Hana Skalická</option>
<option>Simona Kolá?ová</option>
<option>Kate?ina Sychová</option>
<option>Amálka Sychová</option>
<option>Jana Sychová</option>
<option>Magdaléna Sychová</option>
<option>Tereza Sychová</option>
<option>Bohdana Sychová</option>
</select>
Run Code Online (Sandbox Code Playgroud)
这是我的JavaScript:
//inicialization of select picker
$('.selectpicker').selectpicker();
//on change function i need to control selected value
$('select.selectpicker').on('change', function(){
var selected = $('.selectpicker option:selected').val();
alert(selected);
});
Run Code Online (Sandbox Code Playgroud)
我的问题是改变功能不起作用.它没有做任何事情,我自己找不到解决方案.
如果我把它放入文件准备而不是改变功能它似乎工作.所以我想我在某处犯了错误:
$('select.selectpicker').on('change', function(){
Run Code Online (Sandbox Code Playgroud)
我也尝试过只使用:
$('.selectpicker').on('change', function(){
Run Code Online (Sandbox Code Playgroud)
没有这个选择前缀但没有任何变化.
解决方案是将jquery代码放入document.ready()
.感谢Kartikeya
我在这段代码中遇到错误,我正在尝试做一个事件,当页面加载时,它将执行事件.但问题是当我转到其他函数,但同一页面时,它会在该变量上出现null错误.执行此代码时没有问题,但是当我在代码的其他部分时,会发生此错误.
未捕获的TypeError:无法读取null的属性"value"
$(document).ready(function(){
var str = document.getElementById("cal_preview").value;
var str1 = document.getElementById("year").value;
var str2 = document.getElementById("holiday").value;
var str3 = document.getElementById("cal_option").value;
if (str=="" && str1=="" && str2=="" && str3=="" )
{
document.getElementById("calendar_preview").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("calendar_preview").innerHTML=xmlhttp.responseText;
}
}
var url = calendar_preview_vars.plugin_url + "?id=" + str +"&"+"y="+str1+"&"+"h="+str2+"&"+"opt="+str3;
xmlhttp.open("GET",url,true);
xmlhttp.send();
});
Run Code Online (Sandbox Code Playgroud) 我试图调用我在头文件中添加的JS函数.请在下面找到显示我的问题场景的代码.注意:我无法访问应用程序中的正文.每次我点击元素id="Save"
时只会调用f1()
但不是fun()
.我怎么能打电话给我fun()
?请帮忙.
<!DOCTYPE html>
<html>
<head>
<script>
document.getElementById("Save").onclick = function fun()
{
alert("hello");
//validation code to see State field is mandatory.
}
function f1()
{
alert("f1 called");
//form validation that recalls the page showing with supplied inputs.
}
</script>
</head>
<body>
<form name="form1" id="form1" method="post">
State:
<select id="state ID">
<option></option>
<option value="ap">ap</option>
<option value="bp">bp</option>
</select>
</form>
<table><tr><td id="Save" onclick="f1()">click</td></tr></table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 嗨我正在尝试在javascript中使用函数setTimeout(),除非它不起作用.提前感谢任何可以提供帮助的人.
<!DOCTPYE html>
<html>
<head>
<script>
var button = document.getElementById("reactionTester");
var start = document.getElementById("start");
function init() {
var startInterval/*in milliseconds*/ = Math.floor(Math.random() * 30) * 1000;
setTimeout(startTimer(), startInterval);
}
function startTimer() {
document.write("hey");
}
</script>
</head>
<body>
<form id="form">
<input type="button id=" reactionTester" onclick="stopTimer()">
<input type="button" value="start" id="start" onclick="init()">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) document.getElementById('id of div that definately exists')
返回null.
我最初加载了javascript以确保我不需要担心onload事件.我也尝试过使用onload事件.这非常怪异.任何想法或帮助将不胜感激.