2

我正在尝试在我的网站上的一堆链接中创建一个事件侦听器。这些链接是在一个循环中生成的,所以我最终得到了<a class = "replyButton" id = "replyID"<? echo $x; ?>etc。

我正在尝试使用下面的代码在单击每个相应链接时显示一个输入框,但没有运气。在一种情况下,我也可以使用纯 JS 使其工作,但不使用 JQuery,在这样的几个中推断。任何帮助都会非常棒。

window.onload = function(){

    $('.replyButton').each(function(index){
    var domElementId = "replyArea" + index;
    domElementId.onclick = function() {
    var replyFieldHtml = '<div class = "span4" id = "replyTextField"><table><tr><td id = "replyPhoto"><img src = "/assets/img/usr/profile_holder.jpg" /></td><td id = "replyContent"><input type = "text" id = "replyInput" onkeydown="if (event.keyCode == 13) wallReply()" placeholder = "leave a reply..." /></td></tr></table></div>';  
    document.getElementById('domElementId').innerHTML = replyFieldHtml;
    console.log('domElementId');
    return false;
    }
}); 
}

编辑:这是我用来生成 html 的循环... $x = 0; 而 ($x < 8){ $x++; $r = $wallarray - $x;

$postContent = $wall_content['wall_posts'][$x-1];
$postUser = getUserNameById($wall_content['userID'][$x-1]);
?>

<div class = "row">
    <div class = "span6">
        <div class = "span1" id = "wallPhoto"><img src ="assets/img/usr/profile_holder.jpg></div>
        <div class = "span4">
            <div class = "span4" id = "wallFeedStyle"><a id = "wallUserLink" href = "#"><b><? echo $postUser; ?></b></a></div>
        <div class = "row">
            <div class = "span5">
                <div class = "span4" id = "userPost"><? echo $postContent; ?></br><p class = "wallsmall"><a href="#" id = "postLike"></i>Like</a> &middot;<a class = "replyButton" id = "replyButton<? echo $x; ?>" href="#"></i>Reply</a></p></div></div>
            </div>
            <div class = "row">
                <div class = "span5">
            </div>
        </div>  
        <div class = "row" id = "replyArea<? echo $x; ?>"></div>
</div>
<? 
}
?>
4

2 回答 2

4

您以错误的方式使用变量。尝试这个:

window.onload = function () {
    $('.replyButton').each(function (index) {
        var domElementId = "replyArea" + index;
        $('#' + domElementId).on('click', function () {
            var replyFieldHtml = '<div class = "span4" id = "replyTextField"><table><tr><td id = "replyPhoto"><img src = "/assets/img/usr/profile_holder.jpg" /></td><td id = "replyContent"><input type = "text" id = "replyInput" onkeydown="if (event.keyCode == 13) wallReply()" placeholder = "leave a reply..." /></td></tr></table></div>';
            $(this).html(replyFieldHtml);
            console.log(domElementId);
            return false;
        });
    });
}
于 2013-02-02T08:51:37.323 回答
0

在深入挖掘了 .on() 和 .bind() 背后的历史之后,我最终使用下面的代码来解决这个问题。感谢你的帮助!

$('a.replyButton').on("click", function(){
var index = $(this).attr('id');

$('#replyArea'+index).html('<div class = "span4" id = "replyTextField"><table><tr><td id = "replyPhoto"><img src = "/assets/img/usr/profile_holder.jpg" /></td><td id = "replyContent"><input type = "text" id = "replyInput" onkeydown="if (event.keyCode == 13) wallReply()" placeholder = "leave a reply..." /></td></tr></table></div>');
});

我最终将“replyLink”ID 属性更改为数字。所以有一堆/<.a> 类replyButton,ID 属性为数字。而且它似乎做得很好,也不需要设置 .each() 循环。

于 2013-02-03T08:07:35.807 回答