I have this HTML code.
我有这个HTML代码。
<div class="dontsplit" onmouseover="selCode('c111');">
Face savoring delicious food <span class="notranslate" id="c111">????</span>
</div>
I want to select the value between span tags when mouse hovers the div. The code I am using is this.
我想在鼠标悬停div时选择span标签之间的值。我正在使用的代码就是这个。
function selCode(objId) {
if(document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(objId));
range.select();
} else if(window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(objId));
window.getSelection().addRange(range);
}
}
But it doesn't seem to work. How do I get this work?
但它似乎没有用。我该如何完成这项工作?
I repeat. I need to automatically select the text between span when mouse hovers over the div. So it can be copied easily. Thanks in advance!
我重复。当鼠标悬停在div上时,我需要自动选择跨度之间的文本。所以它可以很容易地复制。提前致谢!
3 个解决方案
#1
1
Your script is working for the first mouseover but the second one inwards is giving the error Discontiguous selection is not supported.
so clear the existing selection before doing a new selection.
您的脚本正在进行第一次鼠标悬停,但第二次向内提供错误不支持不连续选择。在进行新选择之前,请清除现有选择。
function selCode(objId) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(objId));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(objId));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
Demo: Fiddle
#2
1
I think this should help.
我认为这应该有所帮助。
You can use jQuery .text()
method.
您可以使用jQuery .text()方法。
You have a span
with notranslate
class.
你有一个不翻译类的跨度。
So you can do somthing like this to get the text:
所以你可以做这样的事情来得到文本:
$(".notranslate").text();
#3
0
Try this one. I'm using jquery to make this task done.
试试这个。我正在使用jquery来完成这项任务。
HTML
<div class="dontsplit">
Face savoring delicious food <span class="notranslate" id="c111">????</span>
</div>
JS
$(function(){
$('.dontsplit').on('mouseover','.notranslate', function(){
console.log($(this).text()); //see result
})
})
#1
1
Your script is working for the first mouseover but the second one inwards is giving the error Discontiguous selection is not supported.
so clear the existing selection before doing a new selection.
您的脚本正在进行第一次鼠标悬停,但第二次向内提供错误不支持不连续选择。在进行新选择之前,请清除现有选择。
function selCode(objId) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(objId));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(objId));
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
Demo: Fiddle
#2
1
I think this should help.
我认为这应该有所帮助。
You can use jQuery .text()
method.
您可以使用jQuery .text()方法。
You have a span
with notranslate
class.
你有一个不翻译类的跨度。
So you can do somthing like this to get the text:
所以你可以做这样的事情来得到文本:
$(".notranslate").text();
#3
0
Try this one. I'm using jquery to make this task done.
试试这个。我正在使用jquery来完成这项任务。
HTML
<div class="dontsplit">
Face savoring delicious food <span class="notranslate" id="c111">????</span>
</div>
JS
$(function(){
$('.dontsplit').on('mouseover','.notranslate', function(){
console.log($(this).text()); //see result
})
})