我有一个输入,然后我按下,这个事件被称为:
document.getElementById("nearest").addEventListener('click', function(){
x$("#popup").toggleClass('hide');
}, false);然后我在控制台中得到这个错误:
x$("#popup").toggleClass is not a function
[Break On This Error] x$("#popup").toggleClass('hide'); 这是div和input元素
<div id="popup" class="hide"></div>
<input id="nearest" type="image" name="nearest">发布于 2011-12-01 23:05:13
下面的代码看起来像预期的那样工作-它将背景颜色从红色切换到蓝色(在FireFox 8.0和Chrome17.0.942.0中测试过):
<html>
<head>
<style type="text/css">
#popup{
position:relative;
height:100px;
width:100px;
margin:10px;
background-color:red;
}
.hide{
background-color:blue !important;
}
</style>
<script type="application/javascript" src="http://xuijs.com/downloads/xui-2.3.2.min.js"></script>
<script type="application/javascript">
x$.ready(function() {
document.getElementById('nearest').addEventListener('click', function(){
x$("#popup").toggleClass('hide');
}, false);
});
</script>
</head>
<body>
<div id="popup" class="hide"></div>
<input id="nearest" type="image" name="nearest" />
</body>
</html>为了确认一下,我假设您最初使用的document.getElementId()是函数的一部分,还是包含在某个onLoad脚本中?在工作示例中,我使用了XUI ready()函数来确保DOM已加载。
同样值得注意的是,下面的代码是等效的,因为您使用的是XUI:
x$.ready(function() {
x$('#nearest').click(function(){
x$("#popup").toggleClass('hide');
});
}); https://stackoverflow.com/questions/7887573
复制相似问题