doqvzh345334 2017-04-25 00:33
浏览 218
已采纳

.load()不显示加载的内容

I have a scrip that when the user selects a category it will load the content in a div. I also have a script that if you click a pre tag will select and copy the text. The thing is that is not working when the content has been loaded, btw sorry for my english here a example and here here is my code

$(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>

This is the content of the test.html:

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

2条回答 默认 最新

  • duankui6150 2017-04-25 00:58
    关注

    Ok, i understood, try sth like :

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿