Javascript jQuery关闭preventDefault()
我有一个页面,其中有一个“获取方向按钮”。标签的href为#,因此它是自链接的。当用户单击此按钮时,我有一个脚本,如果链接仍设置为“#”,则该脚本将阻止默认行为。然后,它使用url中的一些信息将ajax请求整合到我的数据库中,以获得您正在查看其页面的公司的地址。然后它使用地理位置来获取用户的位置,然后使用该位置替换按钮的href属性 当函数在document.ready上运行时,我让它完全工作,但是浏览器会在单击按钮之前请求获得位置的权限,这是不可取的,因此我将它绑定到单击事件 我在一切结束时调用trigger(click),但它仍然阻止默认设置。当我第二次点击一个按钮时,它会做它应该做的,但是我的触发器不工作。我的问题是:有没有一种方法可以使它在函数运行后,触发器(单击)实际上就像第二次单击一样工作。我的代码如下:Javascript jQuery关闭preventDefault(),javascript,html,jquery,jquery-events,preventdefault,Javascript,Html,Jquery,Jquery Events,Preventdefault,我有一个页面,其中有一个“获取方向按钮”。标签的href为#,因此它是自链接的。当用户单击此按钮时,我有一个脚本,如果链接仍设置为“#”,则该脚本将阻止默认行为。然后,它使用url中的一些信息将ajax请求整合到我的数据库中,以获得您正在查看其页面的公司的地址。然后它使用地理位置来获取用户的位置,然后使用该位置替换按钮的href属性 当函数在document.ready上运行时,我让它完全工作,但是浏览器会在单击按钮之前请求获得位置的权限,这是不可取的,因此我将它绑定到单击事件 我在一切结束时调
<div class="row">
<div class="medium-11 medium-centered columns text-center">
<hr />
<a href="#" target="_blank" class="get-directions button" style="border-radius: 10px; font-family: oswald, sans-serif; FONT-SIZE: 14PT;">VIEW MAP & GET DIRECTIONS</a>
</div>
</div>
<script>
$('.get-directions').click(function(event) {
console.log($(this).attr("href"));
var linkHREF = $(this).attr("href");
console.log(linkHREF);
if (linkHREF == "#")
{
console.log("PREVENTED");
event.preventDefault();
var clientPID = window.location.href;
clientPID = clientPID.match(/\/webapp\/p\/[0-9]+/);
clientPID = clientPID[0].replace("/webapp/p/", "");
postData = {"pageID": clientPID};
var startingLocation;
var destination;
$.ajax({
dataType: "json",
data: postData,
type: "POST",
url: "/dealerLocator/getDirectionsButton.php",
success: compileAddress
});
}//END IF PREVENT
function compileAddress(jsonData)
{
destination = jsonData;
destination = destination.replace("#", "");
// check if browser supports geolocation
if (navigator.geolocation) {
if (navigator.geolocation.getCurrentPosition){
// get user's current position
navigator.geolocation.getCurrentPosition(function (position) {
// get latitude and longitude
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
startingLocation = latitude + "," + longitude;
// send starting location and destination to goToGoogleMaps function
goToGoogleMaps(startingLocation, destination);
});
}
}
}
// go to Google Maps function - takes a starting location and destination and sends the query to Google Maps
function goToGoogleMaps(startingLocation, destination) {
$getDirectionsLink = "https://maps.google.com/maps?saddr=" + startingLocation + "&daddr=" + destination;
$(".get-directions").attr("href", $getDirectionsLink);
console.log($(".get-directions").attr("href"));
$(".get-directions").trigger("click");
}
});
</script>
$('.get directions')。单击(函数(事件){
log($(this.attr(“href”));
var linkHREF=$(this.attr(“href”);
console.log(linkHREF);
如果(linkHREF==“#”)
{
控制台日志(“阻止”);
event.preventDefault();
var clientPID=window.location.href;
clientPID=clientPID.match(/\/webapp\/p\/[0-9]+/);
clientPID=clientPID[0]。替换(“/webapp/p/”,”);
postData={“pageID”:clientPID};
var启动位置;
var目的地;
$.ajax({
数据类型:“json”,
数据:postData,
类型:“POST”,
url:“/DealLocator/getDirectionsButton.php”,
成功:穿礼服
});
}//如果阻止,则结束
函数compileAddress(jsonData)
{
目的地=jsonData;
destination=destination。替换(“#”和“”);
//检查浏览器是否支持地理定位
if(navigator.geolocation){
if(navigator.geolocation.getCurrentPosition){
//获取用户的当前位置
navigator.geolocation.getCurrentPosition(函数(位置){
//获取纬度和经度
变量纬度=位置坐标纬度;
var经度=position.coords.longitude;
起始位置=纬度+“,”+经度;
//将起始位置和目标发送到GoToLogleMaps函数
GoToLogleMaps(起始位置、目的地);
});
}
}
}
//转到谷歌地图功能-获取起始位置和目的地,并将查询发送到谷歌地图
函数GoToLogleMaps(起始位置、目的地){
$getDirectionsLink=”https://maps.google.com/maps?saddr=“+startingLocation+”&daddr=“+destination;
$(“.get directions”).attr(“href”,$getDirectionsLink);
console.log($(“.get directions”).attr(“href”);
$(“.get directions”).trigger(“click”);
}
});
如果要禁用默认事件,只需返回false即可。不确定我是否理解这里的所有内容,但请尝试使用href=“javascript:void(0)”
而不是href=“#”
。这样,您就不必首先阻止默认行为。