2012-03-19 34 views
1

什么是禁用文本选择的div和它的所有子元素的语法是什么。我使用$(“#MyContent).disableSelection()来禁用下面的代码中的所有文本,它可以在firefox中工作,并且一次禁用所有这三行。Javascript/Jquery语法来禁用Div和所有子元素上的文本选择?

在IE浏览器9中不起作用(),$(“#text2).disableSelection(),$(”#text3).disableSelection(),所以要禁用所有的文本我必须做$(“#text1).disableSelection()什么是一次禁用或应用到所有儿童的语法?

<div id="MyContent"> 
    <div id="text1">Hello World</div> 
    <div id="text2">Goodbye</div> 
    <div id="text3">Asdf</div> 
</div> 
+0

@gideon,那些不完全相同,因为他的问题是解决有关Internet Explorer的问题。 – 2012-03-19 03:31:37

+0

@奥斯汀是的,我很抱歉雷,是的,我误解了这个问题。 – gideon 2012-03-19 03:36:57

回答

1

这个工作对我来说:

$('#MyContent').children().each(function(i, c) { 
    disableSelection(c); 
}); 

function disableSelection(target) { 
    console.debug(target); 

    if (typeof target.onselectstart != "undefined")    // For IE 
     target.onselectstart = function() { return false }; 
    else if (typeof target.style.MozUserSelect != "undefined") // For Firefox 
     target.style.MozUserSelect = "none"; 
    else              // All other routes (Opera, etc.). 
     target.onmousedown = function() { return false }; 

    target.style.cursor = "default"; 
} 

演示http://jsfiddle.net/9vLFD/4/

+0

大部分都适用。在IE中,如果您开始选择MyContent外部的文本并将其拖入MyContent中,您仍然可以选择MyContent内部的文本。 – Ray 2012-03-19 16:51:49

0

你可以尝试

$('#MyContent').children().each(function(i,c){ 
    $(c).disableSelection(); 
}); 
+0

这似乎没有工作。 – Ray 2012-03-19 03:36:23

+0

这甚至需要通过'each'调用吗?也许只是'.children()。disableSelection()'应该是好的。或强大的捕获所有:'$('#MyContent *')。disableSelection()'!!!/evillaugh – 2012-03-19 03:41:12

+0

谢谢,但那些在IE浏览器不工作。 – Ray 2012-03-19 03:47:22

相关问题