将javascript代码转换为jquery代码时出错
Error while converting javascript code to jquery code
我有下面的代码,其中调用了openNav函数。
<div id="main">
<h2>Sidenav Push Example</h2>
<p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
<span id="sp1" style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
我有运行良好的的Javascript代码
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
}
我试图在jquery中转换它,但它显示错误openNav没有定义。
<script type="text/javascript">
$('#sp1').click(function openNav() {
$('#mySidenav').style.width = "250px";
$('#main').style.marginLeft = "250px";
})
</script>
它可以正确编译和运行,但当我单击open (onclick event)
时,它会抛出一个异常,即openNav未定义。
请解释一下问题出在哪里?感谢
Pure jQuery-您需要使用css
方法
$('#mySidenav').css( "width", "250px" );
$('#main').css( "margin-Left", "250px");
您的方法-或者简单地使用[0]
索引从jquery对象访问DOM元素
$('#mySidenav')[0].style.width = "250px";
$('#main')[0].style.marginLeft = "250px";
<html>
<head>
<title>Sample Page</title>
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="main">
<h2>Sidenav Push Example</h2>
<p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
<span id="sp1" style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
<script type="text/javascript">
function openNav() {
$('#mySidenav').css( "width", "250px" );
$('#main').css( "margin-Left", "250px");
}
</script>
</body>
</html>
更改
<script type="text/javascript">
$('#sp1').click(function openNav() {
$('#mySidenav').style.width = "250px";
$('#main').style.marginLeft = "250px";
})
</script>
收件人:
<script type="text/javascript">
$('#sp1').click(function(){
$('#mySidenav').css({"width":"250px"});
$('#main').css({"magin-left":"250px"});
})
</script>
并从您的html中删除onclick
属性,它在这种方法中是过时的
如果您想使用jquery,请从html:中删除点击功能
<div id="main">
<h2>Sidenav Push Example</h2>
<p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
<span id="sp1" style="font-size:30px;cursor:pointer">☰ open</span>
</div>
Jquery:
<script type="text/javascript">
$('#sp1').click(function() {
$('#mySidenav').css('width','250px');
$('#main').css('margin-left','250px');
});
</script>
如果使用$('#sp1').click(function openNav() { ..})
,您可能会看到一个错误openNav未定义
function openNav() {
$('#mySidenav').css( "width", "250px" );
$('#main').css( "margin-Left", "250px");
}
检查此jsFiddle
尝试删除opennav
:
<script type="text/javascript">
$('#sp1').click(function() {
$('#mySidenav').css('width', "250px");
$('#main').css('marginLeft', "250px");
})
</script>
相关文章:
- 我在代码点火器中出错
- 将javascript代码转换为jquery代码时出错
- 我的Javascript代码出错
- 在Tomcat中运行Javascript代码时出错
- 不允许的关键字符.通过 Codeigniter 中的 jQuery 在帧中加载嵌入代码时出错
- 运行Javascript继承代码时出错
- 将js代码转换为jQueryMobile时出错
- 将Flask中的变量显示为javascript代码时出错
- 代码在shell中运行,但提供'可以't查找变量'从文件运行时出错
- 我的代码出错
- 如何将鼠标悬停在链接上时显示demo_test.txt的内容?我哪里出错了,我的代码应该是什么样子
- 使用 qunit 测试我的代码时出错
- 使用 Lodash 合并数组,为什么结果是重复的数组值?,我的代码在哪里出错
- 语法加载具有现代化 JavaScript 代码的 html 页面时出错
- 从 JavaScript 函数调用 C# 方法背后的代码时出错
- Ajax 客户端框架运行 HTML 代码时出错
- 为什么这个依赖于HTML5地理位置的javascript代码出错
- Javascript:“;无法读取属性“”;name”;未定义的“;本地javascript代码出错
- Javascript和编程新手.我的代码出错了
- 使用javascript的代码出错