標簽中想用widow.open打開一個新頁面(原頁面不刷新),在外部js文件中加入了return false;語句原頁面也仍會跳轉。
想讓大神幫我找找問題。
#js代碼#
window.onload = prepareLinks;
function prepareLinks(){
var links = document.getElementsByTagName("a");
for(var i=0;i<links.length;i++){
if(links[i].getElementsByClassName("class") == "popup"){
links[i].onclick = function(){
popUp(this.getAttribute("href"));
return false;
}
}
}
}
function popUp(winURL){
window.open("02.html","popup","width=500px,height=400px");
}
#html代碼#
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>01</title>
<link type="Text/css" rel="stylesheet" href="../css/01.css">
<link type="text/css" rel="stylesheet" href="../css/example.css">
<script type="text/javascript" src="../js/example.js" ></script>
</head>
<body>
<div id="02">
<h1>some words</h1>
<p class="sunny">
Today is such a sunny day,it links me to the song
"Yellow".Like its word:"They are all yellow",the
world look so shiny and beautiful,i feel myself in
a wonderworld.
</p>
</div>
<h1>Snapshots</h1>
<ul>
<li>
<a href="../images/img2/01.jpg" onclick="showPic(this);return false;" title="01">01</a>
</li>
<li>
<a href="../images/img2/02.jpg" onclick="showPic(this);return false;" title="02">02</a>
</li>
<li>
<a href="../images/img2/03.jpg" onclick="showPic(this);return false;" title="03">03</a>
</li>
<li>
<a href="../images/img2/04.jpg" onclick="showPic(this);return false;" title="04">04</a>
</li>
<li>
<a href="../images/img2/05.jpg" onclick="showPic(this);return false;" title="05">05</a>
</li>
<li>
<a href="../images/img2/06.jpg" onclick="showPic(this);return false;" title="06">06</a>
</li>
<li>
<a href="../images/img2/07.jpg" onclick="showPic(this);return false;" title="07">07</a>
</li>
<li>
<a href="../images/img2/08.jpg" onclick="showPic(this);return false;" title="08">08</a>
</li>
<li>
<a href="../images/img2/09.jpg" onclick="showPic(this);return false;" title="09">09</a>
</li>
<li>
<a href="../images/img2/10.jpg" onclick="showPic(this);return false;" title="10">10</a>
</li>
<br>
<img id="placeholder" src="../images/img2/iconfont-tianjia2.png" alt="pic show">
<p id="info">Here is picInfo</p>
<a id="open" href="02.html" onclick="popUp()">Example</a>
</ul>
</body>
</html>
#下面是調試截圖#
你根本就沒添加上onclick事件
if(links[i].getElementsByClassName("class") == "popup")這句為false,搞清楚你條件是什麼先,getElementsByClassName是得到子節點集合
如果是獲取a的class屬性,直接
if(links[i].className == "popup")
下面那個open也有問題,要這樣寫
<a id="open" href="02.html" onclick="popUp();return false;">Example</a>