0

所以我从错误开始,因为 e.preventDefault 在 IE7 中不起作用。我在这里发现event.preventDefault() 函数在 IE 中不起作用并实现了答案 - 问题是在哪里

event.returnValue = false; 

似乎对那里的其他人都有效,但对我无效!我也装了

if (event.returnValue) {
    alert();
}

并没有得到任何警报。这是我的代码

$('#share a').click(function(e){

   if (event.preventDefault) {
        event.preventDefault();
    } else {
        window.event.returnValue = false;
    }

    if ($(this).parent('li').hasClass('email')) {
        window.open(this.href,'share-this','height=750,width=500,status=no,toolbar=no');
    } else {
        window.open(this.href,'share-this','height=400,width=500,status=no,toolbar=no');
    }
}

当我单击该链接时,我在 IE7 中的三元运算符行上得到一个无效参数错误。我知道它正在命中 event.returnValue = false; 因为我在那里放了一个警报来调试。

有任何想法吗?

4

2 回答 2

1

Your code looks as though you're using jQuery. If you're handling an event that was bound using a jQ function, like on, delegate or bind, jQ will wrap the event object for you. You should have access to jQ's preventDefault(), stopPropagation() and stopImmediatePropagation() methods, rendering your ternary irrelevant.
If you're binding the event yourself, make sure the event variable isn't undefined:

elem.onclick = function(e)
{//e should contain the event object, except for IE<9
    e = e || window.event;//old IE's set the event as a property on the global object
};

If you find this a ropey, and who can blame you, you could augment the Event.prototype. I do this for code that needs to support old versions of IE, and it hasn't let me down yet...

The ternary operator can't be sure how to evaluate the statement event.preventDefault ? event.preventDefault() : event.returnValue = false. You might mean for the JS engine to interpret the code as (event.preventDefault ? event.preventDefault() : event.returnValue) = false (assign false to the result of the ternary) or (event.preventDefault ? event.preventDefault() : event.returnValue = false) (treat the entire statement as a ternary expression).

To avoid this ambiguity, group the assignment expression, to let JS know that the assignment is the or part of the ternary:

event.preventDefault ? event.preventDefault() :  (event.returnValue = false);

Having said that, using a ternary as a statement is generally considered to be bad practice. a far more common way of writing your code is:

e.returnValue = false;//returnValue is set on Chrome and, I believe FF, anyway
//if some browser Event objects don't have this, there's no harm in adding a property
//if the event object is read-only, the assignment will fail, silently
if (e.preventDefault)
{
    e.preventDefault();
}
于 2013-07-05T11:32:06.787 回答
1

三元运算符不是那样工作的。这个说法:

event.preventDefault ? event.preventDefault() :  event.returnValue = false;

是无效的。你应该使用:

if(event.preventDefault)
  event.preventDefault();
else
  event.returnValue = false;

三元运算符用作 r 值,而不是一般的 if/else 语句替换。

此外,对于 IE < 9,您应该使用全局window.event( window.event.returnValue)。

更新

实际上,三元运算符在 Javascript 中似乎是这样工作的(所以它不是无效的)。根据 ECMA 规范和恕我直言,它仍然是“作弊”,它损害了代码的可读性(至少对于可以区分左值和右值的程序员而言)。

于 2013-07-05T10:53:05.413 回答