Js习题
- 写出下列程序片段(加下划线的部分不是程序片段的组成部分)在指定操作后的执行结果
1、<button id="myBtn">点我</button>
<p id="demo"></p>
<script>
document.getElementById("myBtn").addEventListener("click", function()
{
document.getElementById("demo").innerHTML = "Hello World";
});
</script>
点击button后的结果:
2、<p>该实例使用 addEventListener() 方法在同一个按钮添加两个点击事件。</p>
<button id="myBtn">点我</button>
<script>
var x = document.getElementById("myBtn");
x.addEventListener("click", myFunction);
x.addEventListener("click", someOtherFunction);
function myFunction() { alert ("Hello World!")}
function someOtherFunction() { alert ("该函数也将被执行!")}
</script>
点击button后的结果:
3、<button id="myBtn">点我</button>
<p id="demo"></p>
<script>
var x = document.getElementById("myBtn");
x.addEventListener("mouseover", myFunction);
x.addEventListener("click", mySecondFunction);
x.addEventListener("mouseout", myThirdFunction);
function myFunction()
{
document.getElementById("demo").innerHTML += "鼠标经过!<br>"
}
function mySecondFunction()
{
document.getElementById("demo").innerHTML += "点击!<br>"
}
function myThirdFunction()
{
document.getElementById("demo").innerHTML += "鼠标离开!<br>"
}
</script>
点击button并将鼠标从button上移开后的结果:
4、
<button id="myBtn">点我</button>
<p id="demo"></p>
<script>
var p1 = 5;
var p2 = 7;
document.getElementById("myBtn").addEventListener("click", function()
{
myFunction(p1, p2);
});
function myFunction(a, b)
{
var result = a * b;
document.getElementById("demo").innerHTML = result;
}
</script>
点击button后的结果:
5、
<button id="myBtn">点我</button>
<script>
document.getElementById("myBtn").addEventListener("click", function()
{
this.style.backgroundColor = "red";
});
</script>
点击button后的结果:
6、
<div id="myDiv">
<p id="myP">点击该段落, 我是冒泡</p>
</div><br>
<div id="myDiv2">
<p id="myP2">点击该段落, 我是捕获</p>
</div>
<script>
document.getElementById("myP").addEventListener("click", function()
{
alert("你点击了 P 元素!");
}, false);
document.getElementById("myDiv").addEventListener("click", function()
{
alert("你点击了 DIV 元素!");
}, false);
document.getElementById("myP2").addEventListener("click", function()
{
alert("你点击了 P 元素!");
}, true);
document.getElementById("myDiv2").addEventListener("click", function()
{
alert("你点击了 DIV 元素!");
}, true);
</script>
在文字“点击该段落, 我是冒泡”上点击时的结果:
在文字“点击该段落, 我是捕获”上点击时的结果:
在文字“点击该段落, 我是冒泡”所在div内上方区域点击时的结果:
在文字“点击该段落, 我是捕获”所在div内上方区域点击时的结果:
7、
<div id="myDIV">
<button οnclick="removeHandler()" id="myBtn">点我</button>
</div>
<p id="demo"></p>
<script>
document.getElementById("myDIV").addEventListener("mousemove", myFunction);
function myFunction()
{
document.getElementById("demo").innerHTML = Math.random();
}
function removeHandler()
{
document.getElementById("myDIV").removeEventListener("mousemove", myFunction);
}
</script>
在点击button后在点击时页面的变化:
8、
<ul id="myList"><li>Coffee</li><li>Tea</li></ul>
<button οnclick="myFunction()">点我</button>
<script>
function myFunction(){
var node=document.createElement("LI");
var textnode=document.createTextNode("Water");
node.appendChild(textnode);
document.getElementById("myList").appendChild(node);
}
</script>
点击button后的结果:
9、
<ul id="myList1"><li>Coffee</li><li>Tea</li></ul>
<ul id="myList2"><li>Water</li><li>Milk</li></ul>
<button οnclick="myFunction()">点我</button>
<script>
function myFunction(){
var node=document.getElementById("myList2").lastChild;
document.getElementById("myList1").appendChild(node);
}
</script>
点击button后的结果:
10、
<p id="demo">单击“按钮”获取有关元素的子节点的信息</p>
<button οnclick="myFunction()">点我</button>
<script>
function myFunction(){
var txt="";
var c=document.body.childNodes;
for (i=0; i<c.length; i++){
txt=txt + c[i].nodeName + "<br>";
};
var x=document.getElementById("demo");
x.innerHTML=txt;
}
</script>
点击button后的结果:
11、
<button οnclick="myFunction()">点我</button>
<p id="demo"></p>
<script>
function myFunction() {
var c = document.body.children;
var txt = "";
var i;
for (i = 0; i < c.length; i++) {
txt = txt + c[i].tagName + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
</script>
点击button后的结果:
12、
<style>
.mystyle {
width: 300px;
height: 50px;
background-color: red;
color: white;
font-size: 25px;
}
</style>
</head>
<body>
<p>点击按钮为 DIV 元素添加 "mystyle" 类。</p>
<button οnclick="myFunction()">点我</button>
</p>
<div id="myDIV">
我是一个 DIV 元素。
</div>
<script>
function myFunction() {
document.getElementById("myDIV").classList.add("mystyle");
}
</script>
</body>
点击button后div及其内文字的变化:
13、
<p id="demo">单击“按钮”看我的变化。</p>
<button οnclick="myFunction()">点我</button>
<script>
function myFunction(){
var x=document.getElementById("demo");
var y=x.firstChild.nodeName;
x.innerHTML=y;
}
</script>
点击button后的结果:
14、
<div id="myDIV">
<p>P 元素 - div 中的第一个子元素</p>
<span>Span 元素 - div 中的最后一个子元素</span>
</div>
<button οnclick="myFunction()">点我</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myDIV").firstElementChild.tagName;
document.getElementById("demo").innerHTML = x;
}
</script>
点击button后的结果:
15、
<select id="mySelect" size="4">
<option>Google</option>
<option>Runoob</option>
<option>Taobao</option>
<option>Wiki</option>
</select>
<br><br>
<button οnclick="myFunction()">点我</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").firstElementChild.text;
document.getElementById("demo").innerHTML = x;
}
</script>
点击button后的结果:
16、
<select id="mySelect" size="4">
<option>Audi</option>
<option>BMW</option>
<option>Saab</option>
<option>Volvo</option>
</select>
<br><br>
<button οnclick="myFunction()">点我</button>
<p id="demo"></p>
<script>
function myFunction() {
var c = document.getElementById("mySelect").children;
document.getElementById("demo").innerHTML = c[2].text;
}
</script>
点击button后的结果:
17、
<button οnclick="myFunction()">点我</button>
<h2>我是 h2 元素</h2>
<div>我是 div 元素</div>
<span>我是 span 元素</span>
<script>
function myFunction() {
var c = document.body.children;
var i;
for (i = 0; i < c.length; i++) {
c[i].style.backgroundColor = "red";
}
}
</script>
点击button后的结果:
18、
<p id="demo">单击按钮,看我怎么变</p>
<button id="btn1" onclick="myFunction()">点我</button>
<script>
function myFunction(){
var btn=document.getElementsByTagName("BUTTON")[0];
var x=document.getElementById("demo");
x.innerHTML=btn.attributes[0].value;
}
</script>
点击button后的结果:
19、
<h1 style="color:red;">Hello World</h1>
<p id="demo">单击按钮看页面有何变化。</p>
<button οnclick="myFunction()">点我</button>
<script>
function myFunction(){
var h=document.getElementsByTagName("H1")[0];
h.getAttributeNode("style").value="color:green";
}
</script>
点击button后的结果:
20、
<button οnclick="openWin()">打开窗口</button>
<script>
function openWin() {
var myWindow = window.open("", "", "width=200, height=100");
myWindow.document.write("<p>这是一个新窗口'</p>");
setTimeout(function(){ myWindow.close() }, 3000);
}
</script>
点击button后的结果:
21、
<script>
function myFunction(){
var x=document.getElementById("fname");
x.value=x.value.toUpperCase();
}
</script>
</head>
<body>
输入你的名字: <input type="text" id="fname" οnblur="myFunction()">
</body>
当在输入框中输入”zhangsan”后在页面其他地方点击时,输入框中内容是:
22、
<script>
function myFunction(x){
x.style.background="yellow";
}
</script>
</head>
<body>
输入你的名字: <input type="text" οnfοcus="myFunction(this)">
</body>
点击输入框后的结果:
23、
<script>
function displayResult(x){
var table=document.getElementById(x);
var row=table.insertRow(0);
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
cell1.innerHTML="New";
cell2.innerHTML="New";
}
</script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<td>cell 1</td>
<td>cell 2</td>
</tr>
<tr>
<td>cell 3</td>
<td>cell 4</td>
</tr>
</table>
<br>
<button type="button" οnclick="displayResult(‘myTable’)">点我</button>
</body>
点击一次button后的结果:
24、
<script>
function formReset(){
kk.value="kk"; console.log(kk.value);
}
</script>
<body>
<p>在下面的字段中输入一些文本,然后按重置按钮。</p>
<form >
第一个名字: <input id="kk" type="text" name="fname" value="Donald"><br>
下一个名字: <input type="text" name="lname" value="Duck">
<br><br>
<input type="reset" value="重置" οnclick="formReset()">
</form>
</body>
点击重置按钮后第一个名字的输入框中的结果: 控制台输出结果:
25、
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<p>请点击按钮</p>
<button id="hide">按钮1</button>
<button id="show">按钮2</button>
</body>
点击按钮1后的结果:
26、
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>
<style type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#f00;
border:solid 1px #000;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>
<div id="flip">点我。</div>
<div id="panel">Hello world!</div>
</body>
点击“点我”所在DIV区域后结果: