简体   繁体   English

使用jQuery设置元素的html内容,但具有同一类的多个元素

[英]Set html contents of an element using jQuery but with multiple elements of the same class

As the title says, I'm trying to set the html contents of an element using jQuery. 如标题所示,我正在尝试使用jQuery设置元素的html内容。 The context that I'll be using this is when a user hovers over a row on a table. 我将使用的上下文是当用户将鼠标悬停在表的一行上时。 While hovering a popup will display with information specific to the row. 悬停时将显示一个弹出窗口,其中包含该行的特定信息。 However, because the rows contain the same class jQuery's $().html() class will only display information about the first element that matches that class. 但是,由于行包含相同的类,jQuery的$()。html()类将仅显示有关与该类匹配的第一个元素的信息。

Here's the snippet of code 这是代码片段

$(".row").hover(
  function() {
    $(".popup").html(
      $(".content-to-be-displayed").html()
    );
    $(".popup").show();

    $(window).on("mousemove.tooltip", function(e) {
      var x = (e.clientX + 20) + 'px',
          y = (e.clientY + 20) + 'px';
      $(".popup").css({
        top: y,
        left: x,
      });
    });
  },
  function() {
    $(".popup").hide();
    $(window).off("mousemove.tooltip");
  }
);

HTML in partial 局部HTML

<tr class= "row">
<div class="content-to-be-displayed" style="display: none;">
  <h4>Header:</h4>
  <% example_model.example.each do |example| %>
         <p>
           <%= 'erb plus html content' %>
         </p>
        <% end %>
     </div>
   </tr>

HTML in template 模板中的HTML

 <%= row do %>
 <thead>
 </thead>
 <tbody>
    <div class="popup"></div>
    <%= render partial: 'partial', collection: @data %>
 </tbody>
 <% end %>

Use a combination of $(this) and .find 使用$(this)和.find的组合

Working example: https://jsfiddle.net/osvLs54L/ 工作示例: https//jsfiddle.net/osvLs54L/

HTML HTML

    <div class="row">
        <div class="content-to-be-displayed">
            Weee1
        </div>
    </div>
    <div class="row">
        <div class="content-to-be-displayed">
            Weee2
        </div>
    </div>
    <div class="row">
        <div class="content-to-be-displayed">
            Weee3
        </div>
    </div>

    <div class="popup">

    </div>

JQuery JQuery的

$(".row").hover(
 function() { $(".popup").html( $(this).find('.content-to-be-displayed').html());}
);

EDIT: Your table structure is invalid html and I am impressed your browser is even displaying much of anything. 编辑:您的表结构是无效的html,我印象深刻的是您的浏览器甚至显示很多东西。 Please read here for the proper structure. 请阅读此处了解正确的结构。 https://css-tricks.com/complete-guide-table-element/ https://css-tricks.com/complete-guide-table-element/

In the example you've given, just initiallity make these adjustments so it is valid (adding your content to elements under the elements 在您给出的示例中,仅对初始设置进行了调整,因此它是有效的(将内容添加到元素下的元素

HTML in template 模板中的HTML

 <%= row do %>
 <thead>
 </thead>
 <tbody>
    <tr>
     <td>
      <div class="popup"></div>
     </td>
    </tr>

    <%= render partial: 'partial', collection: @data %>
 </tbody>
 <% end %>

HTML in partial 局部HTML

<tr class= "row">
 <td>
   <div class="content-to-be-displayed" style="display: none;">
    <h4>Header:</h4>
    <% example_model.example.each do |example| %>
         <p>
           <%= 'erb plus html content' %>
         </p>
     <% end %>
    </div>
 </td>
</tr>

I'm not entirely sure what you are trying to do - but I'll give it a try. 我不确定您要做什么,但我会尝试一下。 Don't use separate popup elements (you can, but its not clean). 不要使用单独的弹出元素(可以,但是不干净)。 Try storing the popup data in a data attribute. 尝试将弹出式数据存储在data属性中。

When you are using a class Selector which targets multiple elements you can refer to the current element with the this variable which is pointing to the actual element who triggered the event - this way you can use it to use only its attributes / childrens etc... 当使用针对多个元素的Selector类时,可以使用this变量引用当前元素, this变量指向触发事件的实际元素-这样,您可以使用它仅使用其属性/子元素等。 。

Here is a Quick demo: JSnippet Demo 这是一个快速演示: JSnippet演示

HTML: HTML:

<table>
  <tr class="row" data-popup="Popup Row 1 Data">
    <td>Row 1</td>
    <td>Hover Me</td>
  </tr>
  <tr class="row" data-popup="Popup Row 2 Data">
    <td>Row 2</td>
    <td>Hover Me</td>
  </tr>
  <tr class="row" data-popup="Popup Row 3 Data">
    <td>Row 3</td>
    <td>Hover Me</td>
  </tr>
</table>
<div class="popup"></div>

JS: JS:

$(function(){
  $(".row").hover(
    function() {
      var $this = $(this);
      $(".popup").text($this.data("popup"));
      $(".popup").css({ 
        top: $this.offset().top, 
        left:  $this.offset().left + $this.width() + 5,
        width: $this.width()
      }).show();
    },
    function() {
      $(".popup").hide();
    }
  );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM