JsFiddle:库“加载类型”是指库还是我的代码?

问题描述 投票:0回答:1

这似乎很明显,但是我不确定,documentation is ambiguous

我正在使用jsFiddle(尤其是https://jsfiddle.net/suterma/dmqr5fe2/7/,但问题更笼统)]

$("textarea, input").bind("paste", function(e){
    //...
});

我将jQuery库与它一起使用,无论是将jQuery库的加载类型设置为“ on Load”还是“ head ofbottom”,它的功能都不同。

现在,此加载类型实际上是指jQuery库的加载方式或我的代码片段的加载方式吗?

[我试图使用开发人员模式来向下钻取结果窗格,但是非常麻烦,而且我还没有弄清楚。

javascript jquery onload jsfiddle
1个回答
0
投票

您可以通过检查内部iframe或记录文档的innerHTML来检出。例如,将其添加到您的代码中:

console.log(document.documentElement.innerHTML);

你得到

<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title></title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="robots" content="noindex, nofollow">
  <meta name="googlebot" content="noindex, nofollow">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- LOOK HERE: ----------------------------------------------------------------->
  <script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.js"></script>

    <link rel="stylesheet" type="text/css" href="/css/result-light.css">


  <style id="compiled-css" type="text/css">
      textarea{
  display:block;
  width: 100%;
  height: 200px;
  }

 input{
  display:block;
  width: 100%;
  }
  </style>


  <!-- TODO: Missing CoffeeScript 2 -->

  <script type="text/javascript">//<![CDATA[
    // LOOK HERE: ---------------------------------------------------------------------
    $(window).load(function(){

console.log(document.documentElement.innerHTML);
$("textarea, input").bind("paste", function(e){
  e.stopPropagation();
  e.preventDefault();

  var cd = e.originalEvent.clipboardData;

  var text = cd.getData("text/plain");

  $(this).val(text.trim());
});

    });

  //]]></script>

</head>
<body>
    <textarea id="pastezone" placeholder="Copy someting with leading and trailing spaces here, then it gets trimmed!"></textarea>

<input id="pastebox" placeholder="Copy someting with leading and trailing spaces here, then it gets trimmed!">



  <script>
    // tell the embed parent frame the height of the content
    if (window.parent && window.parent.parent){
      window.parent.parent.postMessage(["resultsFrame", {
        height: document.body.getBoundingClientRect().height,
        slug: ""
      }], "*")
    }

    // always overwrite window.name, in case users try to set it manually
    window.name = "result"
  </script>


</body>

如您所见,库(jQuery)正在立即加载到头部的<script>标签中。相反,您的代码放在$(window).load(function(){回调中。

请注意,文档运行时运行代码的方法取决于所使用的库。例如,如果您取消选择jQuery并使用D3.js(或其他任何库),则您的代码将放置在window.onload = function() {回调中。

© www.soinside.com 2019 - 2024. All rights reserved.