這裏是我的我的HTML:如何找出一個函數是從(JavaScript)的稱爲元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel = "stylesheet" type="text/css" href="styles/css/style.css" />
</head>
<body>
<div>
<select class="option_block" size="5" multiple="1">
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
<option>Option 5</option>
</select>
<div class="button_block" multiple="1">
<button class="move_button" id="MoveAllToRight">>></button>
<button class="move_button" id="MoveToRight">></button>
<button class="move_button" id="MoveToLeft"><</button>
<button class="move_button" id="MoveAllToLeft"><<</button>
</div>
<select class="option_block" size="5"></select>
</div>
<script src="scripts/javascript/script.js"></script>
</body>
</html>
的Adh這裏是我的JS腳本:
(function(){
function move(direction) {
..........
}
function moveAll(direction) {
...........
}
document.getElementById("MoveAllToRight").addEventListener("click", function() {
moveAll('ToRight');
});
document.getElementById("MoveToRight").addEventListener("click", function() {
move('ToRight');
});
document.getElementById("MoveToLeft").addEventListener("click", function() {
move('ToLeft');
});
document.getElementById("MoveAllToLeft").addEventListener("click", function() {
moveAll('ToLeft');
});
})();
我決定不顯示包含move
和moveAll
functon,因爲它顯然是過度的信息。
如何才能訪問包含move
和moveAll
函數內部元素的「div」? 我有一個使用「ID」的解決方案,但它不可擴展。
'的document.getElementById工作(「MoveAllToLeft」) .parentElement' – Redu
在各自的回調中使用'event.currentTarget'('event'參數)或'this',並將它傳遞給'move' /'moveAll'。 – Bergi