2013-02-02 32 views
2

我想在我的网站上的一堆链接上创建一个事件监听器。这些链接在一个循环内生成,所以我最终与<a class = "replyButton" id = "replyID"<? echo $x; ?>

我试图使用下面的代码来揭示一个输入框,当每个相应的链接被点击,但没有运气。我可以在一种情况下使用普通的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环路IM ... $ 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> 
<? 
} 
?> 
+3

向我们展示您拥有的html。 – Adil

+0

你想在单击'.replyButton'时显示输入框? –

+0

我已经添加了html感谢@Adil和是JAck,或者在单击回复按钮时插入该HTML块。 – user1953875

回答

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; 
     }); 
    }); 
} 
+0

+1取消使用'onclick' –

+0

太棒了。谢谢 - 现在就试试这个,让你发表。 – user1953875

+0

它似乎还没有工作。当我删除$('selector')。each()行,并尝试仅仅一种情况时,我似乎在使用$('selector')。on('click'...)调用时也遇到了问题。 – user1953875

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()循环。