qq_冷清一笑倾所爱_03613263
2016-12-20 13:22
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<p id="intro">我的课程</p>
<ul>
<li title="JS">JavaScript</li>
<li title="JQ">JQuery</li>
<li title="">HTML/CSS</li>
<li title="JAVA">JAVA</li>
<li title="">PHP</li>
</ul>
<h1>以下为li列表title的值,当title为空时,新设置值为"WEB前端技术":</h1>
<script type="text/javascript">
var Lists=document.getElementsByTagName("li");
for (var i=0; i<Lists.length;i++)
{
// text=Lists[i].getAttribute('title')
// document.write(text +"<br>");
document.write(Lists[i].getAttribute("title")+"<br>");
if(text=="")
{
Lists[i].setAttribute("title","WEB前端技术");
document.write(Lists[i].getAttribute("title")+"<br>");
}
}
</script>
</body>
</html>
if(text == "") text变量没设置,text改为Lists[i].getAttribute("title")
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<p id="intro">我的课程</p>
<ul>
<li title="JS">JavaScript</li>
<li title="JQ">JQuery</li>
<li title="">HTML/CSS</li>
<li title="JAVA">JAVA</li>
<li title="">PHP</li>
</ul>
<h1>以下为li列表title的值,当title为空时,新设置值为"WEB前端技术":</h1>
<script type="text/javascript">
var Lists = document.getElementsByTagName("li");
for (var i = 0; i < Lists.length; i++) {
var text = Lists[i].getAttribute('title');
if (text === "") {
Lists[i].setAttribute("title", "WEB前端技术");
text = Lists[i].getAttribute('title');
}
document.write(text + "<br>");
}
</script>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<p id="intro">我的课程</p>
<ul>
<li title="JS">JavaScript</li>
<li title="JQ">JQuery</li>
<li title="">HTML/CSS</li>
<li title="JAVA">JAVA</li>
<li title="">PHP</li>
</ul>
<h1>以下为li列表title的值,当title为空时,新设置值为"WEB前端技术":</h1>
<script type="text/javascript">
var Lists=document.getElementsByTagName("li");
var text;
for (var i=0; i<Lists.length;i++)
{
// text=Lists[i].getAttribute('title')
// document.write(text +"<br>");
text=Lists[i].title;
if(text=="")
{
Lists[i].title="WEB前端技术";
}
document.write(Lists[i].title+"<br>");
}
</script>
</body>
</html>
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题