我有一个脚本,当用户选择一个类别时,它会在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>
答案 0 :(得分:0)
好的,我明白了,试试吧:
<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 :(得分:0)
如果load()
,.html
文件位于同一个域中,则无法将.txt
方法用于可以使用加载的外部网址。在这种情况下,您必须使用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)
}
}
});
&#13;
<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>
&#13;