suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Wie ändere ich den Standard-Auslösemechanismus des A-Tag-Attributs des Browsers?

Es ist bekannt, dass die Ausführungsreihenfolge eines Tags das onclick->href-Attribut ist

1

2

3

4

5

6

7

8

9

<code>当点击浏览器a标签的时候,浏览器的默认机制如下:

 

1、触发a的click事件

 

2、读取href属性的值

 

3、如果是URI则跳转

 

4、如果是javascript代码则执行该代码</code>

So ändern Sie diesen Mechanismus, sodass nach Abschluss des Onclick-Ereignisses die URL, die das href-Attribut ausführt, springt. Die Funktion im Onclick-Ereignis sendet eine Ajax-Anfrage und ändert das href-Attribut basierend auf dem Rückgabewert Nachdem Sie das href-Attribut geändert haben, müssen Sie eine neue Seite im aktuellen Browser öffnen

Aktualisierung---------2017.06.30------ ---------------

Nach dem Test habe ich die Ajax-Anfrage auf synchrone Ausführung umgestellt, aber die Onclick-Funktion des a-Tags kann immer noch nicht ausgeführt werden, bevor die href-Aktion ausgeführt wird.

Der Grund kann sein, dass Ajax in eine synchrone Anfrage geändert wird, wodurch andere Vorgänge auf der aktuellen Seite blockiert werden,

Aber der Klick auf das a-Tag wurde abgeschlossen und die nachfolgende href-Aktion wird weiterhin ausgeführt. Die href-Aktion ist zu diesem Zeitpunkt void(0) und die Ajax-Anfrage wurde zu diesem Zeitpunkt noch nicht zurückgegeben

Antworten, das heißt, die Ajax-Synchronisierungsanforderung blockiert nicht die Aktion des a-Tags

Ich freue mich auf bessere Antworten

伊谢尔伦伊谢尔伦2817 Tage vor1891

Antworte allen(9)Ich werde antworten

  • 过去多啦不再A梦

    过去多啦不再A梦2017-07-01 09:14:15

    $('a').click(function(e) {

    1

    <code>e.preventDefault()</code>

    var _ = $(this)
    $.get(xx, function() {

    1

    <code>location.href = _.attr('href')</code>

    });
    })

    Antwort
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-01 09:14:15

    1

    2

    3

    4

    5

    6

    7

    <code class="js">$('a').click(function() {

      var link = this

      $.get(xx, function() {

        location.href = link.href

      });

      return false // 阻止先不跳转

    })</code>

    Antwort
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-01 09:14:15

    在onclick里面使用js跳转页面
    //ajax start
    success:function(){

    1

    2

    3

    <code>//todo。。。。。

     

    window.location.href = 'url'</code>

    }

    Antwort
    0
  • 怪我咯

    怪我咯2017-07-01 09:14:15

    1、禁止a标签跳转 href="javascript:void(0)"
    2、在onclick方法中请求ajax,成功后,将返回值绑定到href上

    Antwort
    0
  • typecho

    typecho2017-07-01 09:14:15

    为什么不先不给href赋值,等请求完了再跳转?

    Antwort
    0
  • 欧阳克

    欧阳克2017-07-01 09:14:15

    1

    2

    <code>1、禁止a标签跳转 href="javascript:void(0)"

    2、其他的在onclick方法中进行</code>

    Antwort
    0
  • 天蓬老师

    天蓬老师2017-07-01 09:14:15

    浏览器所有的是有的默认事件的禁用,都可以用 event.preventDefault() 来阻止,剩下的在你的回调函数里,你可以任意的去操作, 当然如果你需要兼容IE8及以下,可以兼容写法如下:

    1

    2

    <code class="javascript">// event 为你的监听onclick回调函数中传递的参数

    event.preventDefault ? event.preventDefault() : (event.returnValue = false);</code>

    Antwort
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-01 09:14:15

    久等了,请食用

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    <code><!DOCTYPE html>

    <html lang="en">

     

    <head>

        <meta charset="UTF-8">

        <title>Document</title>

    </head>

     

    <body>

        <a href="" target="_blank">click</a>

        <script>

            function aTagCustomEvent(e) {

                var tag = e.target;

     

                //setTimeout模拟异步的ajax请求,时间间隔假设为1秒

                setTimeout(function() {

                    tag.href = 'xxx';

                    tag.onclick = function() {};

                    tag.click();

                    tag.onclick = aTagCustomEvent;

                }, 1000)

                return false;

            }

     

            //为页面中所有a标签设置onclick事件

            var aTags = [].slice.call(document.getElementsByTagName("A"));

            aTags.forEach(function(tag) {

                tag.onclick = aTagCustomEvent;

            })

     

        </script>

    </body>

     

    </html>

    </code>

    Antwort
    0
  • 为情所困

    为情所困2017-07-01 09:14:15

    1

    2

    3

    4

    5

    6

    7

    8

    <code><a href="javscript:;" onclick="doSomething(this);">

     

    function doSomething(obj) {

        if($(obj).attr("href") === "javscript:;") {

            // ... ajax get and set url

            $(obj).attr("target", "_blank").trigger("click");

        }

    }</code>

    Antwort
    0
  • StornierenAntwort