Home >Web Front-end >JS Tutorial >Use JS or || to be compatible with FireFox!_javascript tips

Use JS or || to be compatible with FireFox!_javascript tips

PHP中文网
PHP中文网Original
2016-05-16 19:24:11985browse

Use or || for JS to be compatible with FireFox!_javascript tips

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<li> 
<a href="http://www.blueidea.com/articleimg/bbsimg/smile.gif"/></a> 
<a href="图片地址">打开</a> 
</li> 
<li> 
<a href="http://www.blueidea.com/articleimg/bbsimg/biggrin.gif"/></a> 
<a href="图片地址">打开</a> 
</li> 
<li> 
<a href="http://www.blueidea.com/articleimg/bbsimg/confused.gif"/></a> 
<a href="图片地址">打开</a> 
</li> 
</body> 
</html> 
<script language="javascript" type="text/javascript"> 
document.body.onclick = function(evt){ 
 evt = evt || window.event; 
 var o = evt.target || evt.srcElement; 
 window.open(o.previousSibling.href || o.previousSibling.previousSibling.href); 
 return false; 
} 
</script>

Find document.body.onclick = function(evt),
Under IE, this evt will not exist. But under fireFox (it seems to be under opera as well) this parameter will be passed by default. Under IE, this parameter is null. If you want to be compatible, just write it like this.

Continue down,
evt = evt || window.event;
Under IE, evt will point to: window.event, and under fireFox, it will point to the default parameter.
Because under IE, evt || window.event is equivalent to: null || window.event, the result is still window.event
and under fireFox, it is equivalent to evt || null , the result is evt

Next:
o.previousSibling.href || o. previousSibling.previousSibling.href
The former expression is used under IE, and the latter one is used under FireFox.
Because under IE, XMLDom does not have the attribute preserveWhiteSpace, that is, white space is also regarded as a node, and IE The default is false, that is, the blank is not regarded as a node.

The XMLDom mentioned here does not seem to be related to the above, but under FireFox, previousSibling is blank unless there is no space between the two HTML tags. Any form of space.

52612d67623c690c67885d69529f5c685db79b134e9f6b82c0b36e0489ee08ed
431d1916c2041b7c63a0089dd115c936Open5db79b134e9f6b82c0b36e0489ee08ed
There is a line break between the two 3499910bf9dac5ae3c52d5ede7383485 (a type of space), so under FireFox, take the previous node of the next 3499910bf9dac5ae3c52d5ede7383485 If so, you must use:
o.previousSibling.previousSibling.href

Maybe you still don’t understand it, it doesn’t matter, here is a simple one:

<script> 
function test(p1,p2,p3){ 
 p1 = p1 || 100; 
 p2 = p2 || "xlingFairy"; 

 return "p1 = " + p1 + " p2 = " + p2 + " p3 = " + p3; 
}
alert(test()); 
alert(test(null,null,"blueidea.com")) 
</script>

The above is for JS Or || to be compatible with FireFox!_javascript skills. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn