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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛