❶ js,如果判斷滑鼠在元素內
可以按以下思路步驟來實現:
1、聲明一個變數(如el),用來存放滑鼠所在的元素節點。
2、在body元素上增加一個滑鼠事件監聽
3、使用事件對象中的target屬性,每當滑鼠經過一個元素時,就把它賦值給el變數
任何時候,讀取el變數的值即為滑鼠當前所在的元素。
❷ js怎麼監聽滑鼠是否有操作
<script type="text/javascript">
// 移動了就更新最近一次移動的時間。
document.onmousemove = function(){
window.lastMove = new Date().getTime();
};
window.lastMove = new Date().getTime();//最近一次移動時間
window.setInterval(function(){//每1秒鍾檢查一次。
var now = new Date().getTime();
// 如果超時了
if( now - lastMove > 指定時間 ){
// 自己寫了撒。
}
}, 1000);
</script>
❸ js 監聽滑鼠事件怎麼防止多次
可以第一觸發後就移除滑鼠事件函數,這樣後面就不會再觸發了。
❹ js中監聽iframe點擊事件
在js中,需要監聽iframe的點擊事件,但是因為瀏覽器的同源策略,是無法監聽到的,只能另闢蹊徑去解決它
注意,僅限於pc網站,意思就是必須要有滑鼠移入移出iframe的操作
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Detect IFrame Clicks</title>
<script src="./jquery-3.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var currentObj=Object
var isOverIFrame = false;
function processMouseOut(e) {
console.log('out iframe',e.target);
currentObj=e.target
isOverIFrame = false;
top.focus();
}
function processMouseOver(e) {
// log("IFrame mouse >> OVER << detected.");
console.log('in iframe',e.target);
currentObj=e.target
isOverIFrame = true;
}
function processIFrameClick(event) {
if(isOverIFrame) {
console.log('click iframe',event.target,currentObj);
}
}
function attachOnloadEvent(func, obj) {
if(typeof window.addEventListener != 'undefined') {
window.addEventListener('load', func, false);
} else if (typeof document.addEventListener != 'undefined') {
document.addEventListener('load', func, false);
} else if (typeof window.attachEvent != 'undefined') {
window.attachEvent('onload', func);
} else {
if (typeof window.onload == 'function') {
var oldonload = onload;
window.onload = function() {
oldonload();
func();
};
} else {
window.onload = func;
}
}
}
function init() {
var element = document.getElementsByTagName("iframe");
for (var i=0; i<element.length; i++) {
element[i].onmouseover = processMouseOver;
element[i].onmouseout = processMouseOut;
}
if (typeof window.attachEvent != 'undefined') {
top.attachEvent('onblur', processIFrameClick);
}
else if (typeof window.addEventListener != 'undefined') {
top.addEventListener('blur', processIFrameClick, false);
}
}
attachOnloadEvent(init);
});
</script>
</head>
<body>
<iframe src="https://www.hao123.com" width="80%" height="600px"></iframe>
<iframe src="https://www..com" width="80%" height="600px"></iframe>
</form>
</body>
</html>
復制上邊整段html,打開console控制台,即可解決這個問題,親測好用
❺ JS—事件類型(焦點事件、滑鼠事件(部分))
【需要注意】雖然focus與blur不冒泡,但是卻可以在捕獲階段偵聽到它們。
只定義了div2即棕色的那個div的事件
【解釋enter與leave】:當滑鼠由粉移向棕色時,顯示enter,滑鼠由棕移向綠時,不顯示leave與enter。當移出粉色時,顯示leave。
【解釋over與out】:當滑鼠由粉移向棕色時,顯示over,滑鼠由棕移向綠時,顯示out在顯示over,滑鼠由綠移向棕時,顯示out載顯示over。為什麼會顯示呢?是因為事件冒泡,由棕移向綠時會觸發div3的over事件,但是div3並沒有定義,所以向上冒泡到div2。所以會顯示div2的over事件。
【總結】mouseenter與mouseleave是IE提出的事件,不冒泡。但是mouseover與mouseout事件則通過簡單的e.stopPropagation()也無法避免冒泡的現象。
❻ 什麼用javascript讓一個對象同時監聽滑鼠單擊和滑鼠雙擊事件
<input name="test" id="test" type="button" onmouseup="c()" value="單雙擊測試"/>
注意不是onclick,是內onmouseup
var count = 0;
var timer ;
function c(){
count ++;
timer = window.setTimeout(function(){
if(count==1) alert("單擊容");
else alert("雙擊");
window.clearTimeout(timer);
count=0;
},300)
}