gpt4 book ai didi

javascript - 我想把这个随机函数改为onclick函数

转载 作者:行者123 更新时间:2023-11-28 04:58:00 25 4
gpt4 key购买 nike

我实际上是在页面上随机创建圆圈,我想绘制这些 onclick 函数。但是我拥有的代码是随机函数,它随机分布在页面上。那么如何将此代码更改为 onclick ?

<!DOCTYPE html> 
<html lang='en'>
<head>
<html>
<input type="text" name="test" style="width: 130px; height: 30px "value="" id="text" placeholder="Enter length"/>
</html>
<meta charset='utf-8' />
<script type='text/javascript' src='OpenLayers.js'></script>
<script type='text/javascript'>

var map;
var vector_layer;

function init() {
//Create a map with an empty array of controls
map = new OpenLayers.Map('map_element');

//Create a base layer
var wms_layer = new OpenLayers.Layer.WMS(
'OpenLayers WMS',
'http://vmap0.tiles.osgeo.org/wms/vmap0',
{layers: 'basic'},
{}
);
map.addLayer(wms_layer);

//Add vector layer
vector_layer = new OpenLayers.Layer.Vector('Settlement Vector Layer');
map.addLayer(vector_layer);

var settlement_values = {

4: 'circle'
}

//Create some points
for(var i=0; i<20; i++){
vector_layer.addFeatures([new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(
(Math.floor(Math.random() * 360) - 180),
(Math.floor(Math.random() * 180) - 90)
),
{
'settlement_type': settlement_values[(Math.floor(Math.random() * 15))]
}
)]);
}

//Create a style map object
var vector_style_map = new OpenLayers.StyleMap({});

//ADD RULES
//We need to create a 'lookup table' that contains the desired values
// and corresponding symbolizer



var x=document.getElementById("text").value;





var symbolizers_lookup = {

'circle': {
'fillColor': '#336699','fillOpacity':.8, 'pointRadius':x, 'strokeColor': '#003366', 'strokeWidth':2
}
}

//Now, call addUniqueValueRules and pass in the symbolizer lookups
vector_style_map.addUniqueValueRules('default', 'settlement_type', symbolizers_lookup);

//Add the style map to the vector layer
vector_layer.styleMap = vector_style_map;

if(!map.getCenter()){
map.zoomToMaxExtent();
}

}

</script>
</head>

<body onload='init();'>
<div id='map_element' style='width: 600px; height: 600px;'></div>
</body>
</html>

最佳答案

body 标记中将 onload 更改为 onclick

<body onclick='init();'>  <!-- <======== HERE  -->
<div id='map_element' style='width: 600px; height: 600px;'></div>
</body>
</html>

关于javascript - 我想把这个随机函数改为onclick函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42384480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com