Jquery 在if/else语句中,e.preventDefault()未对条件执行100%
此函数(使用Jquery 在if/else语句中,e.preventDefault()未对条件执行100%,jquery,Jquery,此函数(使用选项:选中的)更改0的CSS和HTML,以模拟一种进度条应用程序。问题是它超过了100%。那么如果语句不正确或者这里有什么错误?有什么线索吗 $(function(){ $('#selectChoice').change(function() { $('option:selected', this).each(function() { var id = $('#selectChoice option:selected').attr('i
选项:选中的)更改0
的CSS和HTML,以模拟一种进度条应用程序。问题是它超过了100%
。那么如果语句不正确或者这里有什么错误?有什么线索吗
$(function(){
$('#selectChoice').change(function() {
$('option:selected', this).each(function() {
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').css("color", "#f00").css("font-weight", "bold");
});
});
$('#plus25').on('click', function() {
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').html(function(i, value) {
var newWidth = parseInt(value * 1 + 25);
var e = window.event;
$('#' + id + 'Item').css('width', newWidth + '%');
if (value <= 75){
return newWidth;
} else {
e.PreventDefault();}
});
});
});
可以看到完整的版本您应该使用传递到单击
处理程序的事件
对象,而不是全局窗口。事件
。此外,该方法是小写的preventDefault()
。试试这个:
$('#plus25').on('click', function(e) { // Note the 'e' parameter here
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').html(function(i, value) {
var newWidth = parseInt(value * 1 + 25);
$('#' + id + 'Item').css('width', newWidth + '%');
if (value <= 75) {
return newWidth;
} else {
e.preventDefault();
}
});
});
$('#plus25')。在('click',函数(e){//注意此处的'e'参数
var id=$('#selectChoice选项:selected').attr('id');
$('#'+id+'Item').html(函数(i,值){
var newWidth=parseInt(值*1+25);
$('#'+id+'Item').css('width',newWidth+'%');
if(value)下面的IE bug修复是由于@amitthk:else{safePreventDefault(e);}}}});safePreventDefault=function(evt){//IE bug修复evt=evt | | window.event;if(evt){evt.returnValue=false;}if((evt)&&(evt.preventDefault){evt.preventDefault();}};获取e未定义
。使用函数(e)
.removevar e=window.event;
谢谢@Parth Trivedi.maybevar e=evt | | | | window.event;
需要在其他地方定义吗?谢谢@Rory McCrossan,但是它仍然超过了100%使用newWidth<100
而不是值<75
。我认为如果//else
需要更多的条件。。我到底想问什么呢?也许否则如果你最好开始问一个新问题,因为这个问题是关于preventDefault()
$('#plus25').on('click', function(e) { // Note the 'e' parameter here
var id = $('#selectChoice option:selected').attr('id');
$('#' + id + 'Item').html(function(i, value) {
var newWidth = parseInt(value * 1 + 25);
$('#' + id + 'Item').css('width', newWidth + '%');
if (value <= 75) {
return newWidth;
} else {
e.preventDefault();
}
});
});