我只是对我发现的这个JS感到好奇

I'm just curious about this JS I found

本文关键字:JS 好奇 发现      更新时间:2023-09-26

我在某处找到了这个Javascript,想学习。因此,我只会对我不知道的问题提出问题。

1)我想知道为什么构建器变量有主题标签。

 (function($) {
$(document).ready(function() {
  'use strict';
   var builder = $('#builder');

2)似乎写在那里的建造者与 #builder 有关......

 /**
    * Gets the json for the form in the embedded builder.
    *
    * @return string the form's json.
    */
 var getFormDocument = function() {
     if (document.getElementById('builder').contentWindow.getFormDocument == null) {
        return '{}';
     }
     return document.getElementById('builder').contentWindow.getFormDocument();
   };

3)再次,这里的哈希标签。是来自 css 吗?

     /**
        * Adjusts the builder's height to the window.
        */
   var resizer = function() {
     var w = $(window).height(),
       h = $('header').height(),
       t = $('#top-row').height(),
       p = 12, // top padding
       o = (w - h - t - p) + 'px';
      builder.css('height', o);
  };

4) .js表单文档是 JQuery 的功能之一吗?

/**
   * Called when the user is done building their form.
   */
  var clickedSave = false;
  var save = function() {
      clickedSave = true;
      jQuery('.js-form-document').val(getFormDocument());
      jQuery('#builderModal_signup').reveal();
  };

5)为什么有TODO说iframe会发疯?在用户保存表单后,这个js代码如何连接到数据库?

/**
   * Adds listeners for the embedded builder and keeps the
   * builder properly positioned.
   */
  var init = function() {
    // TODO: iframe width at 100% goes bonkers.
    var width  = $($('div.twelve.columns')[0]).width();
    builder.css('display', 'block');
    builder.css('width', width + 'px');
    $('.js-save').click(save);
  };

 var hasFields = function() {
    var formDocument = JSON.parse(getFormDocument());
    if (formDocument.fields == null) {
      return false;
    }
    var sections = formDocument.fields;
    if (sections.length === 0 || sections[0].fields.length === 0) {
        return false;
    } else {
        return true;
    }
  };

6)不明白这里的哈希标签是什么意思...

/*
   * Triggers a call to action for the user
   */
  var firstCallout = function() {
    if (clickedSave) {
      return;
    }
    if (!hasFields()) {
        jQuery('#builderModal_noFields').reveal();
    } else {
        jQuery('#builderModal_hasFields').reveal();
    }
  };

 init();
  setTimeout(function(){firstCallout();}, 30000);
  setInterval(function() {
      var onClass = $('.js-save').attr('data-on-class');
      if (onClass === '') {
        onClass = 'green';
      }
      if (hasFields()) {
        $('.js-save').removeClass('gray').addClass(onClass);
      } else {
        $('.js-save').removeClass(onClass).addClass('gray');
      }
  }, 1000); 
});
})(jQuery);
var lastHeight = 0;
function resizeBuilder() {
  setInterval(function() {
    var iframe = document.getElementById('builder');
    var padding = 20;
    var height = iframe.contentWindow.document.body.scrollHeight;
    //chrome counts padding in scroll height and creates never ending height
    if (Math.abs(height - lastHeight) <= padding) {
        return;
    }
    height += padding;
    if (height < 400) {
        height = 400;
    }
    iframe.height = height + 'px';
    lastHeight = height;
  }, 1000);
}

显然你缺乏jQuery的基本知识。哈希代表 DOM 元素 ID 的选择器,点代表类选择器。关于"TODO" - 这看起来像是开发人员在受够了 iframe 后的评论。为什么它会在 100% 上发疯?毫无头绪。

表单正在由客户端脚本与之通信的某些服务器端脚本/代码处理(阅读有关 AJAX 的更多信息)。

希望澄清一点,祝你今天开心

不是"hashtag"是一个CSS选择器,他通过id"builder"选择对象

在 CSS 中,您可以使用此语法选择内容。

  • "#" 按 ID
  • "." 按类
  • "sometag",没有特殊的caracter,这意味着您正在选择一个标签(或一堆标签)。

使用标签、类或 id,您可以使用以下属性在选择中执行

"div[name=mydiv]"。

希望对您有所帮助。