Ⅰ 求一段js腳本:當DIV失去焦點後隱藏自身。而當DIV內部元素被點擊的時候先觸發點擊事件再觸發失去焦點事件
呵呵,我也遇到過同樣的問題,一開始的想法是在彈出的DIV上用事件隱藏自己,如果這個DIV中沒有其它內容的話是可以實現的,但如果在彈出的div中嵌套有其它div或其它標簽的話,就會有問題。記得我好像使用了一個笨辦法,就是先獲取這個彈出的DIV的范圍,同時獲取滑鼠的當前位置,當滑鼠移出DIV的范圍的時候,隱藏DIV。以下是這段代碼,也共同期待有更好的辦法。 還需要說明的是這段代碼在IE上沒問題,如果是FireFox的話,獲取滑鼠當前位置應該用:event.pageX|Y,這個問題在代碼中加一個判斷應該可以解決。
<script language="javascript" type="text/javascript">
document.onmousemove = mouseouthide;
//滑鼠移出DIV范圍時隱藏
function mouseouthide() {
var b=20;//加一點餘量,否則當點擊文本框時div沒彈出就被隱藏了
var obj = document.getElementById("poplist"); //poplist是彈出div的ID號
var t = document.body.offsetTop;
var l = document.body.offsetLeft;
var ol = l + obj.offsetLeft-b;
var or = l + obj.offsetLeft + obj.offsetWidth + b;
var ot = t + obj.offsetTop-b;
var ob = t + obj.offsetTop + obj.offsetHeight + b;
var x = event.clientX + document.body.scrollLeft;
var y = event.clientY + document.body.scrollTop;
if (x <= ol || x >= or || y <= ot || y >= ob)
obj.style.display = "none";
}
</script>
Ⅱ js中<input/>如何獲得焦點
用自帶的()就可以了
利用js中<input/>實現文本框默認獲取輸入焦點完整代碼實現如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
//輸入框獲取滑鼠焦點
function autoFocus(){
var pFocus = document.getElementById("password");
pFocus.focus();
pFocus.select();
}
</script>
</head>
<body onload="autoFocus()">
<div id="loginform">
<h1 style="font-size:1.5em;padding:20px;">輸入密碼</h1>
<form action="${pageContext.request.contextPath}/" method="post">
<input id="password" type="password" name="password">
<input type="submit" value="提交">
</form>
</div>
</body>
</html>
(2)js獲取焦點事件用於div擴展閱讀:
JavaScript 使我們有能力創建動態頁面。事件是可以被 JavaScript 偵測到的行為。 網頁中的每個元素都可以產生某些可以觸發JavaScript函數的事件。比方說,我們可以在用戶點擊某按鈕時產生一個 onClick 事件來觸發某個函數。事件在HTML頁面中定義 。
JavaScript官方API介面-:focus
網路-JavaScript
W3cschool-獲得欄位焦點
Ⅲ 在css樣式中能不能直接設定某個元素獲得焦點如一個<div>在頁面載入完之後焦點就在他上面
寫jquery吧
假設你要設為焦點的id為yyy,例如<input type='text' id='yyy' />
然後在頁面里先引入Jquery,然後自定義函數
<script type='text/javascript'>
$(function(){
yyy.focus();
});
</script>
Ⅳ js獲取div內容
假設你抄的html中有以下的div
<div id="id1">
</div>
<div id="id2">
</div>
如果是要獲取id是id1的div的數據,獲取div裡面的內容有兩種方式
1,純js獲取數據
var a=document.getElementById("id1").innerText;
2,jqeury獲取數據
var a=$("#id1").html();
Ⅳ HTML和JS中所謂的「焦點」是指什麼
焦點在HTML和JS中是只游標。
焦點在JS和HTML里是在頁面上屏幕中閃動的小豎線,滑鼠點擊就可獲得游標,Tab鍵可按照設置的Tabindex來進行切換焦點。
示例:
<divid="demo"></div>
<divid="test"></div>
<divid="one"></div>
<divid="two"></div>
<divid="three"></div>
<divid="fore"></div>
<divid="five"></div>
<divid="six"></div>
<script>
function$(id){
returndocument.getElementById(id);
}
$("demo").style.backgroundColor="green";
//調用方法
$("test").style.backgroundColor="blue";
$("one").style.backgroundColor="orange";
$("two").style.backgroundColor="red";
$("three").style.backgroundColor="purple";
$("fore").style.backgroundColor="#f6e71f";
$("five").style.backgroundColor="#5153ff";
$("six").style.backgroundColor="#ff1496";
//調用函數,並直接修改盒子的背景顏色
(5)js獲取焦點事件用於div擴展閱讀
jquery判斷input輸入框的值
//輸入框正在輸入時
$("#ipt").on('input',function(){
if(!($('#ipt').val()=='')){
$(".cancle_ico").removeClass('hide');
}else{
$(".cancle_ico").addClass('hide');
}
})
//輸入框得到焦點時
$("#ipt").on('focus',function(){
if(!($('#ipt').val()=='')){
$(".cancle_ico").removeClass('hide');
}else{
$(".cancle_ico").addClass('hide');
}
})
//輸入框失去焦點時
$("#ipt").on('blur',function(){
if(($('#ipt').val()=='')){
$(".cancle_ico").addClass('hide');
}else{
$(".cancle_ico").removeClass('hide');
}
})
Ⅵ js如何實現:在頁面中任意位置點擊滑鼠,則在該位置顯示一個div
如下參考:
1.頁面復制有一個id為c2的div,下面有一個子div,下面有一個按鈕,事件是用來點擊100個adddiv。
Ⅶ 點擊js彈出div層,當滑鼠移出div層窗口後任意點擊關閉div層窗口!大蝦們應該怎麼實現啊啊啊
div對象.onclick=function(ev){ev = ev || window.event; ev.cancelBubble = true;}
document.documentElement.onclick=function(ev){這里寫關閉div的代碼}
不懂再問我
Ⅷ JS如何獲取某個DIV下的元素
1、新建一個html文件,命名為test.html,用於講解Jquery如何獲得div下的元素。
2、在test.html文件內,使用div標簽創建一個模塊,在div內,使用p標簽,span標簽創建測試的內容。
3、在test.html文件內,設置div的id屬性為divcon,主要用於下面通過該id獲得div對象。
4、在test.html文件內,使用button標簽創建一個按鈕,按鈕名稱為「獲得div下的元素」。
5、在test.html文件中,給button按鈕綁定onclick點擊事件,當按鈕被點擊時,執行divfun()函數。
6、在js標簽中,創建divfun()函數,在函數內,通過id(divcon)獲得div對象,使用html()方法便可以獲得div對象下面的元素了。最後,使用alert()方法輸出元素。
7、在瀏覽器打開test.html文件,點擊按鈕,查看結果。
總結
1、創建一個test.html文件。
2、在文件內,在div標簽內,使用p標簽創建一行文字,同時創建一個button按鈕,用於觸發執行js函數。
3、在js標簽內,創建函數,在函數內,使用getElementById()方法通過id(mydiv)獲是div對象,再使用getElementsByTagName()方法獲得div下面的p元素對象,最後,使用alert()方法輸出p元素的內容。