2

我有一些从 mysql 输入的内容,单击时应该触发警报()...但是,这不起作用...

这是我从 php/get_answers.php 输入的代码的代码

<?php session_start(); ?>
<?php require_once("../includes/include_all.php"); ?>
<?php $answers = $question->get_answers_for_question($_GET['id']); ?>
<?php while($row = $answers->fetch_array()){ ?>
  <!-- ALL ANSWERS HERE -->
  <div class = 'answer-row'>
    <div class = 'answer-side'>
        <div class = 'arrow-contain-answer' type = 'answer' id = 'arrow-up' answer-id = '<?php echo $row["id"]; ?>'></div>
        <div class = 'answer-votes-contain'>
            <?php echo $row['popularity']; ?>
        </div>
        <div class = 'arrow-contain-answer' id = 'arrow-down'answer-id = '<?php echo $row["id"]; ?>'></div>
      </div>

    <div class = 'answer-content'>
      <?php 
        echo $row['content'];
      ?>
    </div>

    <div class = 'actions'>
        <a href = '#' class= 'add-comment' id = '<?php echo $row["id"]; ?>'> add comment </a>
    </div>

  </div>

<?php } ?>

这是它显示的页面上的jquery:

$(".arrow-contain-answer").click(function(){
    alert();
});

我想要发生的是当有人单击具有“箭头包含答案”类的元素时,将发生事件..

我认为在通过 mysql/php 将元素“馈入”到页面之前,我遇到了问题。

4

2 回答 2

4
$(document).on("click", ".arrow-contain-answer", function(){
    alert();
});

尝试这种方式来动态添加元素!

于 2013-04-04T08:08:17.507 回答
1

如果在添加动态馈送元素时将其委托给文档中存在的最接近的静态元素(父级),则效果会更好(性能方面)。

$('.answer-row').on("click", ".arrow-contain-answer", function(){
  alert('clicked');
});

阅读有关委派事件的更多信息

于 2013-04-04T08:14:20.723 回答