代码之家  ›  专栏  ›  技术社区  ›  Igor Chernega

bootrstrap行中的所有内容都不可点击`

  •  2
  • Igor Chernega  · 技术社区  · 9 年前

    我使用此函数在API响应上创建卡片:

    function createCard(name, img, description) {
    $('#result').append(`
      <div class="col-md-4 col-sm-6 col-xs-12">
        <div class="card" style="height: 200px; border: 2px solid black; border-radius: 5px; overflow: hidden; padding: 15px; margin: 15px 0;">
          <div class="card-content" style="height: 100%; overflow: hidden;">
            <div class="star" style="height: 1.6em;">
              <span class="glyphicon glyphicon-star-empty pull-right"></span>
            </div>
            <div class="row">
              <div class="image col-xs-4" style="margin-top: 10px;">
                <figure style="height: 120px;">
                  <img src=${img} alt="" style="max-width: 100%; max-height: 100%;">
                </figure>
              </div>
              <div class="description col-xs-8">
                <h4>${name}</h4>
                <p>${description}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      `);
    }
    

    这是 result 块:

    <div id="result" class="row"></div>
    

    下面是jquery事件处理程序:

    $('.star').on('click', function() {
      console.log("!")
    });
    

    问题是没有控制台日志,不仅仅是 star 块,但与任何其他内部 在没有问题的情况下阻止 后果 它本身

    顺便说一句,在上应用悬停伪类样式是绝对好的 明星

    row 上课不方便?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Soolie    9 年前

    您的内容是动态加载的。因此,您需要将事件委托给静态父级。

    $(document).on('click', '.star', function() {
      console.log("!")
    });
    

    如上例所示,请使用静态父对象,而不是 document . 使用 文件 当没有静态父对象时。

    $(function() {
      $(document).on('click', '.star', function() {
        console.log("!")
      });
    });
    
    function createCard (name, img, description) {
      $('#result').append(`
      <div class="col-md-4 col-sm-6 col-xs-12">
        <div class="card" style="height: 200px; border: 2px solid black; border-radius: 5px; overflow: hidden; padding: 15px; margin: 15px 0;">
          <div class="card-content" style="height: 100%; overflow: hidden;">
            <div class="star" style="height: 1.6em;">
              <span class="glyphicon glyphicon-star-empty pull-right"></span>
            </div>
            <div class="row">
              <div class="image col-xs-4" style="margin-top: 10px;">
                <figure style="height: 120px;">
                  <img src=${img} alt="" style="max-width: 100%; max-height: 100%;">
                </figure>
              </div>
              <div class="description col-xs-8">
                <h4>${name}</h4>
                <p>${description}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
      `);
    }
    
    createCard ("Hello World 1!", "//placehold.it/100", "Hello World 1!");
    createCard ("Hello World 2!", "//placehold.it/100", "Hello World 2!");
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="result" class="row"></div>

    这个 console.log 当我单击上面的代码段时,它会起作用:

    console