问题描述
我有一个HTML锚点,当点击时应该让div上下滑动(我称之为 .slideToggle();
function);
我的问题:当我点击链接而不是执行代码时,它会进入一个新页面,其中url是javascript代码,在我的情况下, 我的代码: javascript:$('#attachFileContainer')。slideToggle();
并且页面(body)的内容就是 [object Object]
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而不是转到新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!