jQuery 'this'修饰符和if/else语句
jQuery issues with the 'this' modifier and if/else statement
好的,所以我试图使它,使图像可以修改或移动在浏览器中点击按钮在页面的一边,但在我的脚本如果/else语句不工作,就像我想要它。以下是我到目前为止在jQuery中所做的:
<meta charset="utf-8">
<title>Image Mod</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="js/main.js">
var menu = $('ul li').click(function() {
console.log($(this).index());
if (menu = 0) {
$('this').css('width', '80%')
}
else if (menu = 1) {
$('this').css('height', '80%')
}
else if (menu = 2) {
$('this').css('height', '80%')
}
else if (menu = 3) {
$('this').css('top', '30')
}
else if (menu = 4) {
$('this').css('left', '50')
}
else if (menu = 5) {
$('this').css('left', -50')
}
else {
}
});
</script>
</head>
<body>
<nav id="sidebar">
<ul>
<li>Edit 1</li>
<li>Edit 2</li>
<li>Edit 3</li>
<li>Edit 4</li>
<li>Edit 5</li>
<li>Edit 6</li>
</ul>
</nav>
<img class="active" src="images/picture.jpg" />
</body>
$('this')是
改为$(this)
在if语句中,单个=是一个赋值,如果右边的值非零,它将计算为true。您需要使用==来测试是否相等。您应该考虑一个switch语句,如下所示:
switch ($(this).index()) {
case 0: // Stuff
break;
case 1: // More stuff
break;
default:
// No other match
}
基本上,你错了
- 在事件处理程序 之外执行if/else语句
- 做模仿而不是比较 当你想检查
- 检查处理程序的返回值
$('li').index();
时,对于这个给定的HTML:
<nav id="sidebar">
<ul>
<li>Edit 1</li>
<li>Edit 2</li>
<li>Edit 3</li>
<li>Edit 4</li>
<li>Edit 5</li>
<li>Edit 6</li>
</ul>
</nav>
<section>
<img class="active" src="http://lorempixel.com/400/200/" />
</section>
<section>
是必需的,因为您想要更改<img>
的高度
这个给定的CSS:
#sidebar li {cursor: pointer;}
#sidebar li:hover {color: purple;}
section {height: 200px;}
img {height: 100%; position: relative;}
您需要使用以下jQuery代码:
jQuery(function($) {
$('#sidebar').find('li').on('click', function() {
var position = $(this).index(), // the position of the <li> in the <ul>
css = {
property: null,
value: null
}, // prepare to store CSS properties and values to apply to the img
$img = $('img'); // the img on which CSS changes are applied
// define CSS properties and values according to the position of the <li> clicked
switch(position) {
case 0:
css.property = 'width';
css.value = '80%';
break;
case 1:
css.property = 'height';
css.value = '80%';
break;
case 2:
css.property = 'height';
css.value = '80%';
break;
case 3:
css.property = 'top';
css.value = '30px';
break;
case 4:
css.property = 'left';
css.value = '50px';
break;
case 5:
css.property = 'left';
css.value = '-50px';
break;
}
// apply CSS properties
$img.css(css.property, css.value);
});
});
工作JSFiddle
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何在 API 调用后和 if 语句中启用提交按钮
- 带有多个elseif的Javascript if语句
- Sharepoint JScript if语句未执行
- for循环中的javascript if语句找不到==
- jquery if语句返回return wong语句
- 在if语句下的html中使用javascript变量
- 将错误与if语句混淆
- 循环通过数组的If语句不起作用
- jQuery模板中的If语句
- 如何从数据库中回显If语句
- 如何减少if语句的数量
- 在Javascript中将一个值和字符串数组转换为if语句
- Dropbox oauth认证的IF语句的第二部分是't已触发
- MVC Jquery-按钮点击-获取if语句错误
- 如何重写Javascript If语句以选择Classes而不是ID's的HTML格式
- KnockoutJS:为虚拟元素使用嵌套的条件if语句
- If语句发布或操作员发布
- 清除函数中if语句内部不起作用的间隔
- 使用if语句重新循环