简体   繁体   English

如何获得clicked元素的href值?

[英]How can I get the href value of clicked element?

Here is my code: 这是我的代码:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdn.rawgit.com/makeusabrew/bootbox/master/bootbox.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <a onclick="return bootbox.confirm('are you sure?', function(e){ if (e) {window.location = this.href;} });" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">Link</a> 

Noted that I use this library for confirm . 注意,我使用此库进行confirm

But this line doesn't work in my code: 但是此行在我的代码中不起作用:

window.location = this.href;

And it always (when I click OK ) redirects me here: 它总是(当我单击OK时将我重定向到此处:

http://localhost:8000/undefined

How can I fix it? 我该如何解决?

<a onclick="return bootbox.confirm('are you sure?', function(e){ if (e) {window.location.href = $(this).attr('href');} });" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">

Instead of onClick you can do something like this. 代替onClick您可以执行以下操作。

<a id="some-id" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">
$('#some-id').click(function(){
    var link = $(this).attr('href');
    bootbox.confirm('are you sure ?', function(e){
      if(e){
         window.location.href = link;
       }
    })
})

In your code the this.href doesn't work because of this is refering to the bootbox object and this last doesn't have an href property : 在你的代码this.href不起作用,因为this是指的在bootbox对象,这最后没有一个href属性:

What I suggest to you is just using a common function that is used to show the dialog and in the same way you can use several each one redirect to a url. 我向您建议的只是使用一个用于显示对话框的通用函数,并且您可以使用相同的方法将多个重定向到url。 and also and set dynamicly the dialog title by passing it as a parameter, 并通过将其作为参数传递来动态设置对话框标题,

Here is a snippet : ( I have replaced location.href by conosle.log to prevent redirecting ...) 这是一个片段:(我已用conosle.log替换了location.href以防止重定向...)

 function redirect(node,message) { if(!message) message =""; return bootbox.confirm(message, function(e){ if (e) { // we use console instead of redirecting directly console.log(node.href); // location.href = node.href; } }); } 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/> <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> <script src="http://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script> <a onclick="redirect(this,'are you sure ?');" href="http://www.google.com" data-toggle="modal" data-target="#confirm-delete">link 1</a> <br> <a onclick="redirect(this,'leave the page ?');" href="http://www.bing.com" data-toggle="modal" data-target="#confirm-delete">link 2</a> <br> <a onclick="redirect(this,'are you sure to leave !! ?');" href="http://www.yahoo.com" data-toggle="modal" data-target="#confirm-delete">link 1</a> 

Use this code it will give you a pop up if you click on yes then it will processed further otherwise stay on same page. 使用此代码,如果单击“是”,它将弹出一个窗口,否则将进行进一步处理,否则将保留在同一页面上。 Try this. 尝试这个。

<a onclick="return confirm('Are you sure you want to delete this?');" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">

Bootbox is a third party plugin. Bootbox是第三方插件。 You cannot access this pointer inside the bootbox confirm callback function. 您不能在启动箱确认回调函数内部访问此指针。 You have to save this.href in a local variable first before calling bootbox there after use the local variable inside the bootbox confirm callback. 您必须先将this.href保存在本地变量中,然后再在其中使用bootbox确认回调的本地变量后调用bootbox。

<a onclick="NavigatePage()" href="/Remove-Post/{{ $tb }}/{{$post->id}}"/>

function NavigatePage(){
   var URL = this.href;  
   bootbox.confirm('are you sure?', function(UserResponse){
    if (UserResponse) {
      window.location = URL
    } 
   });
 }

In your case you will get undefined because you are accessing this object inside the bootbox callback function. 在您的情况下,您将无法定义,因为您正在Bootbox回调函数中访问this对象。 Inside the call back this object is not accessible. 在回调内部,无法访问this对象。

Is this.ref in scope of the function? this.ref是否在功能范围内? Wrote my last lines of JS two years ago... 两年前写了我的JS最后几行...

Edit: I remember, problem is the return value. 编辑:我记得,问题是返回值。 If it is not flase. 如果不是火炬。 It will move to the href location. 它将移动到href位置。

Do not have your libary, but you can adapt it: 没有您的库,但是您可以对其进行调整:

 <a onclick="if(confirm('are you sure?')) { alert(this.href);};return false;" href="/Remove-Post/{{ $tb }}/{{$post->id}}" data-toggle="modal" data-target="#confirm-delete">Click me</a> 

Edit 2: Before stupidly down voting, you should try the code. 编辑2:在愚蠢地拒绝投票之前,您应该尝试使用该代码。 It works. 有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM