簡體   English   中英

javascript(jQuery)在IE6中不起作用

[英]javascript (jQuery) not working in IE6

我已經開發了一個可拖動的div,其中包含使用jquery的圖像。 該腳本在Firefox,Chrome和IE6中無法正常運行。 你能幫我解決這個問題嗎

在這里檢查網頁: 我的網頁

非常感謝您的考慮。

IE使用clientX和clientY代替pageX和pageY。 有些人通過執行以下操作來解決此問題:

//if IE, then:
if (e.srcElement) {
    e.pageX = oEvent.clientX + document.body.scrollLeft;
    e.pageY = oEvent.clientY + document.body.scrollTop;
}

//rest of event handler goes here

我可能不會自己編寫代碼。 jQuery UI提供了應該可以使用的$(...).draggable()方法,並且該方法已經過跨瀏覽器測試。 您甚至可以自定義構建jQuery UI下載,其中僅包含所需的組件。

http://jqueryui.com/demos/draggable/

http://jqueryui.com/download

除非您期望很多訪問者使用它-否則請放棄IE6支持。 保持站點IE6兼容會增加代碼冗余或降低質量。

由於您已經在使用jQuery,為什么不使用jQuery UI的可拖動組件? 這樣,您不必處理所有鼠標按下的計算。 我將您網站的代碼切換為使用jQuery UI的可拖動功能,而且速度非常快,所需的代碼也少得多。

這是我使用的代碼:

<!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>jQuery Drag and drop</title>
<style type="text/css"> 
#dv {
             position: absolute;
             cursor: move;
           }  
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script language="javascript"> 
    $(document).ready(function() {
        $("#dv").draggable({
            cursor: 'crosshair'
        });
    });
</script>
</head>
<body>

<div id="dv" style="position:absolute;left:300px;top:200px;">
<img src="http://www.mejoyal.com/jquery/drupal.png" />
</div>


</div>
</body>

</html>

希望這可以幫助!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM