Home > Backend Development > PHP Tutorial > 我在discuz的模板里写了一个鼠标事件为什么没有效果呢

我在discuz的模板里写了一个鼠标事件为什么没有效果呢

WBOY
Release: 2016-06-20 12:26:31
Original
933 people have browsed it


<script> <br /> window.onload = function() { <br /> var oBox = document.getElementByclassName("img"); <br /> oBox.onmouseover = function() { <br /> oBox.className = "hover"; <br /> }; <br /> oBox.onmouseout = function() { <br /> oBox.className = ""; <br /> }; <br /> }; <br /> </script>





可是任何效果都没有,不知道是哪里的问题。求请教


回复讨论(解决方案)

getElementsByClassName

getElementsByClassName



没有效果是为什么呢?

getElement sByclassName
不是什么浏览器都支持 getElementsByclassName 的

getElementsByclassName 得到的是一个数组
oBox.onmouseover ???
给数组指定鼠标事件?你这样写是不行的

<div id="WB_Blogger">	<div class="img" style="height:100px;width:100px;background-color:#00ff00;">		xxx	</div>	<div class="img" style="height:100px;width:100px;background-color:#c1c1c1;">		xxx	</div></div><script type="text/javascript">window.onload = function() {	var oBox = document.getElementsByClassName("img");	for (var i = 0, L = oBox.length; i < L; i++) {		oBox[i].onmouseover = function() {			this.className = "hover";		};		oBox[i].onmouseout = function() {			this.className = "";		};	}};</script>
Copy after login
Copy after login

<div id="WB_Blogger">	<div class="img" style="height:100px;width:100px;background-color:#00ff00;">		xxx	</div>	<div class="img" style="height:100px;width:100px;background-color:#c1c1c1;">		xxx	</div></div><script type="text/javascript">window.onload = function() {	var oBox = document.getElementsByClassName("img");	for (var i = 0, L = oBox.length; i < L; i++) {		oBox[i].onmouseover = function() {			this.className = "hover";		};		oBox[i].onmouseout = function() {			this.className = "";		};	}};</script>
Copy after login
Copy after login



找到原因了,单独拿出来写就可以,但是放入到discuz的模板里就不行。没有效果。怎么办呢

貌似discuz对页面的js处理有问题。

最好使用 来引入。

把下面代码放入xxx.js

window.onload = function() {    var oBox = document.getElementsByClassName("img");    for (var i = 0, L = oBox.length; i < L; i++) {        oBox[i].onmouseover = function() {            this.className = "hover";        };        oBox[i].onmouseout = function() {            this.className = "";        };    }};
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template