重定向到另一个页面与创建按钮
Reddirecting to another page with created button
我有这段代码。如果条件满足,我创建一个按钮"显示在地图上"。现在我需要在创建的按钮上运行onclick事件,该按钮将用户重定向到新URL上的地图。我试着用"窗口"。函数go_to_map中的Location ",但它不起作用。任何帮助吗?
function coordinates_Conv (d, m, s) {
return d + m / 60 + s / 3600;
}
function alarm(){
var lat_d = parseInt(document.getElementsByClassName("lat_deg")[0].value);
var lat_m = parseInt(document.getElementsByClassName("lat_min")[0].value);
var lat_s = parseInt(document.getElementsByClassName("lat_sec")[0].value);
var lon_d = parseInt(document.getElementsByClassName("lon_deg")[0].value);
var lon_m = parseInt(document.getElementsByClassName("lon_min")[0].value);
var lon_s = parseInt(document.getElementsByClassName("lon_sec")[0].value);
if ((coordinates_Conv (lat_d,lat_m,lat_s)<=90) && (coordinates_Conv (lat_d,lat_m,lat_s)>=0) && (coordinates_Conv (lon_d, lon_m, lon_s)<=180) && (coordinates_Conv (lon_d, lon_m, lon_s)>=0)){
document.getElementById("vypocet").innerHTML= String(Math.round(coordinates_Conv (lat_d,lat_m,lat_s)*1000)/1000) + "<br>" + String(Math.round(coordinates_Conv (lon_d, lon_m, lon_s)*1000)/1000);
var show_map = document.createElement("button","map");
show_map.setAttribute("id","map");
show_map.setAttribute("onclick", go_to_map);
show_map.innerHTML = "Show on map";
document.body.appendChild(show_map);
}
else {
alert("Invalid input");
}
}
function go_to_map(){
var targetMap = "https://www.mapurl.com/"
window.location=targetMap;
}
您的onclick
处理程序从一开始就不会被调用。
要正确设置处理程序,请直接设置元素的onclick
:
show_map.onclick = go_to_map;
需要将show_map.setAttribute("onclick", go_to_map);
替换为show_map.onclick = go_to_map;
。这是因为onclick
是event
而不是attribute
。
相关文章:
- 创建按钮,根据表单字段中的数据获取特定的URL
- 当我通过.aspx.cs创建按钮时,它不起作用
- 创建按钮不调用表单内的任何操作
- Internet Explorer 8不允许动态创建按钮
- 如何在不加载页面的情况下创建按钮事件
- 使用动态创建按钮单击事件未触发
- “创建”按钮,用于切换不包含特定文本颜色的表行的可见性
- Javascript创建按钮不会显示在HTML中
- 函数创建按钮,带有传递参数的 onclick 函数
- JavaScript jQuery Mobile 在 for 循环中创建按钮
- 火狐工具栏上的创建按钮来执行JS
- 使用 JavaScript 在 for 循环中创建按钮
- 如何创建按钮转到测验软件中的下一个问题
- 需要帮助创建按钮以激活 JavaScript 函数
- 使用javascript从服务器端的名称列表中创建按钮/href
- 如何用JavaScript用onclick创建按钮
- 创建按钮时陷入无休止的循环
- 如何覆盖剑道网格中的创建按钮单击
- 动态创建的DIV在其他创建按钮中的位置
- 创建按钮到函数Javascript