【问题标题】:.load() does not show the content loaded.load() 不显示加载的内容
【发布时间】:2017-04-25 11:55:08
【问题描述】:

我有一个脚本,当用户选择一个类别时,它将加载div 中的内容。我还有一个脚本,如果您单击 pre 标记将选择并复制文本。问题是内容加载后无法正常工作,顺便说一句对不起我的英语here a example,这是我的代码

$(document).ready(function() {
  $("#esto").on("change", function() {
    var vale = this.value
    $("#divcontent").load("http://letraspiolas.com/" + vale + ".html");
  });
});

(function() {
  function selectText(a) {
    var b = document,
      text = a,
      range, selection;
    if (b.body.createTextRange) {
      range = b.body.createTextRange();
      range.moveToElementText(text);
      range.select()
    } else if (window.getSelection) {
      selection = window.getSelection();
      range = b.createRange();
      range.selectNodeContents(text);
      selection.removeAllRanges();
      selection.addRange(range)
    }
  }
  preTags = document.getElementsByTagName("pre");
  for (var i = 0; i < preTags.length; i++) {
    preTags[i].onclick = function() {
      selectText(this);
      document.execCommand("copy")
    }
  }
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>
  <select id="esto" data-placeholder="select a category...">
    <option value="">select</option>
    <option value="test"> test </option>
    <option value="test1">test1</option></select>

  <div id="divcontent">
    <pre>this text can be select and copy</pre>
  </div>
</body>

这是test.html的内容:

<h3>title</h3>
<div class="kghjghjg">
  <pre>____i want to select this</pre>
  <pre>_____and this</pre>
  <div class="clear"></div>
</div>

【问题讨论】:

    标签: javascript php html


    【解决方案1】:

    好的,我明白了,试试看:

    <script type="text/javascript"> 
    
    $(document).ready( function(){
    
        // load event
        $("#esto").on("change", function(){
    
            var vale = $(this).val();
            $( "#divcontent" ).load("http://letraspiolas.com/"+vale+".html", function() {
                console( "Loaded." );
            });
        });
    
        // event on pre tag
        $("body").on("click","pre", function(){
            selectText(this);
            document.execCommand("copy")
        });
    
    });
    
    function selectText(a){
    
        var b=document,text=a,range,selection;
        if(b.body.createTextRange){
            range=b.body.createTextRange();
            range.moveToElementText(text);
            range.select()
        }
        else if(window.getSelection){
            selection=window.getSelection();
            range=b.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range)
        }
    }
    
    </script>
    

    【讨论】:

      【解决方案2】:

      如果.html.txt 文件在同一个域中,则不能对可以使用加载的外部 URL 使用 load() 方法。在这种情况下,您必须使用html() 方法。

      $("#divcontent").html('<object data="http://letraspiolas.com/' + vale + '.html">').promise().done(function() {
        console.log('Loaded');
        initPreTags();
      });
      

      更新: 如果这不是问题,那么您可以使用load(),但调用这样的回调函数:

      $("#divcontent").load("http://letraspiolas.com/" + vale + ".html", initPreTags());
      

      在这种情况下initPreTags() 是内容加载后将执行的回调方法。

      请看一下你修改过的代码:

      $(document).ready(function() {
        $("#esto").on("change", function() {
          var vale = this.value
          $("#divcontent").load("http://letraspiolas.com/" + vale + ".html", initPreTags());
        });
      
        function initPreTags() {
          console.log('Loaded');
          var preTags = document.querySelectorAll('pre');
          preTags.forEach(function(preTag){
            preTag.addEventListener('click', function() {
              selectText(this);
              document.execCommand('copy');
            });
          });
        }
      
      
        function selectText(a) {
          var b = document,
            text = a,
            range, selection;
          if (b.body.createTextRange) {
            range = b.body.createTextRange();
            range.moveToElementText(text);
            range.select()
          } else if (window.getSelection) {
            selection = window.getSelection();
            range = b.createRange();
            range.selectNodeContents(text);
            selection.removeAllRanges();
            selection.addRange(range)
          }
        }
      });
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      
      <body>
        <select id="esto" data-placeholder="select a category...">
          <option value="">select</option>
          <option value="test"> test </option>
          <option value="test1">test1</option></select>
      
        <div id="divcontent">
          <pre>this text can be select and copy</pre>
        </div>
      </body>

      【讨论】:

      • 谢谢,我说错了,我放了那个例子,但它在我的域中,没有使用 http……只是路径,但很高兴知道
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多