从另外两个元素创建一个jquery元素(代码解释请求)

Creating a jquery element from two other elements (code explanation request)

本文关键字:元素 一个 jquery 代码 请求 解释 两个 创建      更新时间:2023-09-26

我想知道是否有人能向我解释以下几行:

var panel = parseInt($el.parent().attr('data-panel'));
var $curr_panel = $('.h5p-panel:eq(' + panel + ')', that.$myDom);
var $next_panel = $('.h5p-panel:eq(' + (panel + 1) + ')', that.$myDom);

如果有人能用简单的英语教我,我会非常感激的

我知道$curr_panel和$next_panel是由h5p-panel中的一个元素和dom元素的组合创建的,但此后我就很困惑了。

代码可以在这里找到:

https://github.com/h5p/h5p-summary/blob/master/js/summary.js#L222

还有一个现场演示:

https://h5p.org/summary

您需要先了解以下内容:

  1. that=>引用到Summary类的一个实例。

    119   Summary.prototype.createQuestion = function() {
    120        var that = this;
    
  2. that.$myDom=>jQuery元素<div class="summary-content"/>

     124 this.$myDom = $('<div>', {
     125       'class': 'summary-content'
     126   });
    
  3. $(A,B)=>表示选择A,其中B是其父级(上下文)之一。[相同B.find(A)]

  4. CCD_ 9=>表示选择具有Css类CCD_;其亲本之一为CCD_ 11。

结论:

$curr_panel = $('.h5p-panel:eq(' + panel + ')', that.$myDom);

意味着,选择DOM元素,其中:

  • 具有Css类=h5p-panel
  • 第三个元素,如果我们假设panel=2。(因为eq(0)=>第一个元素)
  • 它的父母之一是that.$myDom