点击锚点应该执行JavaScript而不是转到新页面 [英] Clicking anchor should execute javascript not go to new page

查看:75
本文介绍了点击锚点应该执行JavaScript而不是转到新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML锚点,当点击时应该让div上下滑动(我称之为 .slideToggle(); function);



我的问题:当我点击链接而不是执行代码时,它会进入一个新页面,其中url是javascript代码,在我的情况下, javascript:$('#attachFileContainer')。slideToggle(); 并且页面(body)的内容就是 [object Object]

我的代码:

>< a class =interactiveLink
href =javascript:$('#attachFileContainer')。slideToggle();>附加档案< / a>

出了什么问题,我有很多锚元素,我从href属性调用javascript,从来没有发生过?

解决方案

您需要这个:

 < a class =interactiveLinkonclick =$('#attachFileContainer')。slideToggle(); return false; href =#>附加档案< / a> 

返回false 阻止链接进行在 onclick 东西之后的任何内容都会运行。


I have a HTML anchor that when clicked should make a div slide up or down (I call JQuery's .slideToggle(); function);

My Problem: When I click the link, instead of executing code, it goes to a new page, where the url is the javascript code, in my case, "javascript: $('#attachFileContainer').slideToggle();" and the contents of the page(body) is simply "[object Object]".

My Code:

<a class="interactiveLink" 
     href="javascript: $('#attachFileContainer').slideToggle();">Attach File</a>

What is going wrong, I have had many anchor elements where I call javascript from the href attribute and this have never happened?

解决方案

You want this:

<a class="interactiveLink" onclick="$('#attachFileContainer').slideToggle(); return false;" href="#">Attach File</a>

The return false prevents the link from doing anything after the onclick stuff runs.

这篇关于点击锚点应该执行JavaScript而不是转到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆