Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

javascript - <a>标签</a>上的preventDefault()(preventDefault() on an <a> tag)

I have some HTML and jQuery that slides a div up and down to show or hide` it when a link is clicked:(我有一些HTML和jQuery,可在单击链接时上下滑动div以显示或隐藏它:)

<ul class="product-info"> <li> <a href="#">YOU CLICK THIS TO SHOW/HIDE</a> <div class="toggle"> <p>CONTENT TO SHOW/HIDE</p> </div> </li> </ul> $('div.toggle').hide(); $('ul.product-info li a').click(function(event){ $(this).next('div').slideToggle(200); } My question is: How do I use preventDefault() to stop the link acting as a link and adding "#" to the end of my URL & jumping to the top of the page?(我的问题是:如何使用preventDefault()停止链接充当链接,并在URL末尾添加“#”并跳至页面顶部?) I can't figure out the right syntax, I just keep getting an error saying(我找不到正确的语法,只是不断出现错误) preventDefault() is not a function.(preventDefault()不是函数。)   ask by Chris J Allen translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try something like:(尝试类似:)

$('div.toggle').hide(); $('ul.product-info li a').click(function(event) { event.preventDefault(); $(this).next('div').slideToggle(200); }); Here is the page about that in the jQuery documentation(这是jQuery文档中有关该页面的页面)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...