Javascript 单击以使用jQuery显示结果
我正在使用这个代码Javascript 单击以使用jQuery显示结果,javascript,php,jquery,slidetoggle,Javascript,Php,Jquery,Slidetoggle,我正在使用这个代码 if ($forum['type'] != 'c' && !$forum['linkto'] && $forum['posts']) { $forum['collapsed_image'] = '<div class="expcolimage"><a href="javascript:void(0);" id="forum_name" fid="'.$fid.'" title="Double click here to
if ($forum['type'] != 'c' && !$forum['linkto'] && $forum['posts'])
{
$forum['collapsed_image'] = '<div class="expcolimage"><a href="javascript:void(0);" id="forum_name" fid="'.$fid.'" title="Double click here to see latest threads of this section."><img src="images/collapse_collapsed.gif" id="ann_'.$forum['fid'].'_img" class="expander" alt="[-]" title="[-]" /></a></div>';
}
else
{
$forum['collapsed_image'] = '';
}
现在,当您双击上面的链接时,jquery代码将运行并显示结果。我不想这样做,我只需要单击一次就可以运行jquery代码来显示结果。如何做到这一点
谢谢 $forum['collapsed_image']='';
$forum['collapsed_image'] = '<div class="expcolimage"><a id="forum_name" fid="'.$fid.'" title="Double click here to see latest threads of this section."><img src="images/collapse_collapsed.gif" id="ann_'.$forum['fid'].'_img" class="expander" alt="[-]" title="[-]" /></a></div>';
从锚定标记中删除href=“javascript:void(0);”
$(document).ready(function() {
$('a[id^="forum_name"]').click(function(event){
代码只需单击一下就可以运行,确保没有任何其他js代码干扰以forum_name开头的id
<div class="expcolimage"><a href="javascript:void(0);" id="forum_name_123" fid="123" title="Double click here to see latest threads of this section."><img src="images/collapse_collapsed.gif" id="ann_123_img" class="expander" alt="[-]" title="[-]" /></a>
使用inspect查看console.log输出您使用e.preventDefault()
的目的是什么?@Arun:代码很好,我问了如何使用它,这样只需单击一次就可以运行代码,而不是单击两次???@user28543,因为您不需要它。如果删除href属性,也不会重定向。请清理浏览器的缓存并运行,然后让我知道是否只需选中remove e.preventDefault();尝试清除缓存并删除e.preventDefault()代码>没什么,您缺少了其他内容。不在这个代码中。停止投票,将所有这些事件向下绑定到外部分区。或者在锚标记之间写一些东西。我认为你的a标签太小了,但它只需点击两次,而不是一次
<div class="expcolimage"><a href="javascript:void(0);" id="forum_name_123" fid="123" title="Double click here to see latest threads of this section."><img src="images/collapse_collapsed.gif" id="ann_123_img" class="expander" alt="[-]" title="[-]" /></a>
jQuery(document).ready(function($){
$('a[id^="forum_name"]').on('click', function (e){
e.preventDefault();
var fid = $(this).attr("fid");
console.log(fid);
$.ajax(
{
type : "post",
dataType: "html",
url : "misc.php?action=forum_threads&fid="+fid,
cache: false,
success : function(response)
{
$("#forum_threads_"+fid).stop().slideToggle("fast").html(response);
}
});
});
});