Heim  >  Artikel  >  Backend-Entwicklung  >  javascript - jquery 如何让a标签不执行跳转?

javascript - jquery 如何让a标签不执行跳转?

WBOY
WBOYOriginal
2016-06-06 20:17:341481Durchsuche

正常情况下,我给a标签的href 添加jacascript:; 就可以阻止它的点击事件
但是这次我是拿到了一个函数,我无法修改里面的href。因为我需要href的地址做ajax加载。
请问我如何设置才能让这个href执行onclick事件而不跳转呢?

回复内容:

正常情况下,我给a标签的href 添加jacascript:; 就可以阻止它的点击事件
但是这次我是拿到了一个函数,我无法修改里面的href。因为我需要href的地址做ajax加载。
请问我如何设置才能让这个href执行onclick事件而不跳转呢?

<code class="javascript">             var _this = $(this);
             var subHref = _this.attr('href');
             e.preventDefault();           
             $.ajax({
                 url: '1.html',
                 data: formVal,
                 type: post,
                 dataType: 'json',
                 success: function(res) {
                     if (res.code == 200) {
                         location.href = subHref;
                     } else {
            
                     }
                 }
                 error: function() {
            
                 }
             });</code>

href="javascript:;" onclick="myfunction("http://shabi.com")"

这样写不就行了,地址非要写在href里吗
做函数参数传递吧

$(“a”).on("click",function(){

<code>return false;
//想干嘛,干嘛
</code>

})

js有个阻止默认行为的函数
e.preventDefault();
你试试可以嘛

其实点击a标签会跳转,是因为点击触发的函数默认返回true,因此当修改onclick函数的返回值为false时,将不会跳转。

<code>var a=document.getElementById("a_id");
a.onclick=function(e){

//do something for myself
return false;
}

</code>

执行函数只写return也可以不执行的

<code>var a=document.getElementById("a_id");
a.onclick=function(e){
return;
}</code>

在click事件里,直接

<code>return false;
</code>

即可

<code><a href="javascript:"></a></code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn