* @see 鼠标点击拖拽的效果(页面可以同时拖动多个框)
* @param boxId 整个对象(框)的id(一般为div或table)
* @param event 内置对象(必须传入)
*/
function mousePlead1(event, boxId) {
var o = getO(boxId)
var isIE = document.all ? true : false
var e = event
var x = e.offsetX || e.layerX
var y = e.offsetY || e.layerY
document.onmousemove = function(e) {
o.style.filter = 'Alpha(opacity=70)'
o.style.opacity = '0.7'
if (isIE) {
o.setCapture()
} else {
window.captureEvents(Event.MOUSEMOVE)
}
var e = window.event || e
if (e.clientX - x >= 0 && e.clientY - y >= 0 && e.clientX - x <= getWinSize()[0] - getO(boxId).offsetWidth
&& e.clientY - y <= getWinSize()[1] - getO(boxId).offsetHeight) {
o.style.left = (e.clientX - x) + "px"
o.style.top = (e.clientY - y) + "px"
}
}
document.onmouseup = function(e) {
document.onmousemove = function() {
}
if (isIE) {
o.releaseCapture()
} else {
window.releaseEvents(o.MOUSEMOVE)
}
o.style.filter = ""
o.style.opacity = ""
}
}
/**
* @see 获得对象
* @param id 对象的id(表单元素和其他标签都可以)
* @return Object
*/
function getO(id) {
return document.getElementById(id)
}
/**
* @see 获得当前窗体的大小(width,height)
* @return Array
*/
function getWinSize() {
var width = parseInt(document.documentElement.clientWidth)
var height = parseInt(document.documentElement.clientHeight)
return new Array(width, height)
}
通常元素的ID获取使用getElementById()方法,实例如下:元素值设定为input:
<input name="input_name" id="input_id" class="password"></input>
在JS中获取ID值也就是“input_id”这个值:
<script type="text/javascript" language="javascript">
var item = document.getElementById("input_id")
</script>
如此,所有为input_id的ID的input对象都在item中了。
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,height=device-height, initial-scale=1.0, maximum-scale=1.0, user-scalable=0, shrink-to-fit=no">
<title>Title</title>
</head>
<body>
<div id="container">
<div id="id_1">textContent_1</div>
<div id="id_2">textContent_2</div>
<div id="id_3">textContent_3</div>
<div id="id_4">textContent_4</div>
<div id="id_5">textContent_5</div>
<div id="id_6">textContent_6</div>
<div id="id_7">textContent_7</div>
<div id="id_8">textContent_8</div>
<div id="id_9">textContent_9</div>
<div id="id_10">textContent_10</div>
<div id="no_1">textContent_no_event_call</div>
<div id="no_2">textContent_no_event_call</div>
<div id="no_3">textContent_no_event_call</div>
<div id="no_4">textContent_no_event_call</div>
<div id="no_5">textContent_no_event_call</div>
</div>
<script type="text/javascript">
//采用代理方式处理
var divs = document.querySelectorAll("div[id^='id_']")
var ids = Array.prototype.slice.call(divs).map(function (div) {
return div.id
})
document.querySelector('#container').addEventListener('click', function (e) {
if (ids.indexOf(e.target.id) > -1) {
alert(e.target.textContent)
}
}, false)
</script>
</body>
</html>