我正在使用 JavaScript 构建一个网站。我有一张传单 map 和一些标记。我在 div 上有一个列表和复选框。我想用复选框过滤标记。但我的 onclick 函数无法到达标记的数据。像这样:
function onclick() {
//delete marker
//i can not reach marker's data from here
}
function initializeMap() {
//something
function markers() {
//my markers defined here
function onclickIwant() {
//something
}
}
}
我想使用 onclickIwant
函数作为复选框 onclick 或从 onclick
函数获取标记的数据。我该怎么办?
我的html:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>title</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
<link rel="stylesheet" href="leaflet.css"/>
</head>
<body>
<div id="harita"></div>
<script src="leaflet.js"></script>
<script src="index.js"></script>
<div id="liste">
<input type="checkbox" id="id1" checked="true" onclick="somefunction();"><label>id1</label>
<input type="checkbox" id="id2" checked="true" onclick="somefunction();"><label>id2</label>
<input type="checkbox" id="id3" checked="true" onclick="somefunction();"><label>id3</label>
<ol id="Listesi"></ol>
</div>
</body>
</html>
最佳答案
我认为你可以这样做:
function onclick() {
//delete marker
//i can not reach marker's data from here
window.onclickIwant();
}
function initializeMap() {
//something
function markers() {
//my markers defined here
window.onclickIwant = function() {
//something
}
}
}
将onclickIwant函数注册到window对象中,您可以在任何地方使用它。
祝你好运!
关于javascript - 另一个函数中的复选框 onclick 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26202962/