我有一个 jQuery 代码,它
$("a.reply").click(function() {
//code
});
当我第一次单击带有 .reply
类的链接时,没有任何反应。第二次点击时,点击函数中的代码起作用了。
正在使用来自 mysql 数据库的 PHP 在页面上插入链接。所以它不是动态插入的。
为什么会这样?有什么解决办法吗?
BadASS 代码:
$(function(){
//TextArea Max Width
var textmaxwidth = $('#wrapper').css('width');
//Initialize Focus ids To Different Initially
var oldcommentid = -1;
var newcommentid = -2;
//End Of initialization
$("a.reply").click(function() {
newcommentid = $(this).attr('id');
if (newcommentid == oldcommentid)
{
oldcommentid=newcommentid;
$("#comment_body").focus();
}
else
{
$('#comment_form').fadeOut(0, function(){$(this).remove()});
var commetformcode = $('<form id="comment_form" action="post_comment.php" method="post"><textarea name="comment_body" id="comment_body" class="added_comment_body" rows="2"></textarea> <input type="hidden" name="parent_id" id="parent_id" value="0"/> <div id="submit_button"> <input type="submit" value="Share"/><input type="button" id="cancelbutton" value="Cancel"/></div></form>');
commetformcode.hide().insertAfter($(this)).fadeIn(300);
//
var id = $(this).attr("id");
$("#parent_id").attr("value", id);
oldcommentid=newcommentid;
//dynamicformcreation function
dynarun();
//
}
return false;
});
dynarun();
function dynarun()
{
//Form Re-Run Functions
$('#comment_body').elastic();
texthover();
$("#comment_form input, select, button").uniform();
textareasizer();
$("#comment_body").focus();
$("abbr.timestamp").timeago();
return false;
}
//TextArea Resizer Function
function textareasizer(){$("#comment_body").css('max-width', textmaxwidth);return false;}
//Other Miscellaneous Functions
$('.comment-holder').hover(
function(event) {
$(this).addClass('highlight');
},
function(event) {
$('.comment-holder').removeClass('highlight');
}
);
function texthover()
{
$('.added_comment_body').hover(
function(event) {
$(this).parent().parent().addClass('highlight');
},
function(event) {
$('.comment-holder').removeClass('highlight');
}
);
return false;
}
});
最佳答案
这是不可能的,但您是否正在运行某种跟踪脚本?像 webtrends 或 coremetrics(或者甚至是您自己的一些脚本,它在全局范围内寻找所有点击)?不久前我遇到了一个类似的问题,初始点击被 coremetrics 捕获。只是一个想法。
关于html - Jquery Click 事件在第一次点击时不触发,但在第二次点击时触发,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5050329/