简体   繁体   English

单击`img`源时,文本选择不会清除

[英]Text selection not clears when click on `img` source

I have a container, and it has children within. 我有一个容器,里面有孩子。 one of the children has the text in it. 其中一个孩子中有text another has the img in it. 另一个有img

the problem is, when user select the text (using mouse, just like selecting text in word doc etc), assume that, the user click on the image source (which is occupied the parent), the text not clearning. 问题是,当用户选择text (使用鼠标,就像在word doc中选择文本一样),假定用户单击图像源(已占据父对象),则text不会清除。

the text keeps selection up to i click out of img source. 文字会一直保持selection直到我从img源中单击为止。 how to fix this issue. 如何解决此问题。

I require, even if the user clicks on img the selection need to cleared. 我要求,即使用户单击img ,也需要清除选择。

 $('.container').on('click', function (e) { var target = $(e.target); //window.getSelection().empty(); i can't use this. console.log(target); //getting img. }); 
 .container{ border:1px solid green; width:300px; height:150px; position:relative; } .imageHolder{ position:absolute; left:0; top:0; } .textHolder { position:absolute; } 
 <div class="container"> <div class="imageHolder"><img src="http://thumb7.shutterstock.com/display_pic_with_logo/599431/127612211/stock-photo-green-apple-isolated-on-white-background-127612211.jpg" width=300 height=150></div> <div class="textHolder">some text goes here</div> </div> 

Can any one help me to solve this? 谁能帮我解决这个问题? possibly I would like to avoid js function, in case if there is a solution in css or html . 如果在csshtml有解决方案,可能我想避免使用js函数。

In case if i use this js function, i am not able to select the text at all.. 如果我使用此js函数,则根本无法选择text

if (window.getSelection) window.getSelection().removeAllRanges();
    else if (document.selection) document.selection.empty();

jsfiddle 的jsfiddle

Try the Following. 请尝试以下。

 $('.container').on('click', function (e) { var target = $(e.target); //window.getSelection().empty(); i can't use this. //console.log(target); //getting img. var txt=$(this).find(".textHolder"); if(target[0].tagName === 'IMG') { txt.addClass('noselect'); txt.attr('unselectable','on'); }else{ txt.removeClass('noselect'); txt.attr('unselectable','off'); } }); $('.container .textHolder').on('mousedown',function(){ $(this).removeClass('noselect'); $(this).attr('unselectable','off'); }); 
 .container{ border:1px solid green; width:300px; height:150px; position:relative; } .imageHolder{ position:absolute; left:0; top:0; } .textHolder { position:absolute; } .noselect { -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="imageHolder"><img src="http://thumb7.shutterstock.com/display_pic_with_logo/599431/127612211/stock-photo-green-apple-isolated-on-white-background-127612211.jpg" width=300 height=150></div> <div class="textHolder">some text goes here</div> </div> 

Maybe this is stupid but I don't understand why you can't attach this event on the .imageHolder ? 也许这很愚蠢,但我不明白为什么您不能在.imageHolder上附加此事件?

 $('.imageHolder').on('click', function(e) { if (window.getSelection.empty) { // Chrome window.getSelection().empty(); } else if (window.getSelection().removeAllRanges) { // Firefox window.getSelection().removeAllRanges(); } else if (document.selection) { // IE? document.selection.empty(); } }); 
 .container { border: 1px solid green; width: 300px; height: 150px; position: relative; } .imageHolder { position: absolute; left: 0; top: 0; } .textHolder { position: absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="imageHolder"> <img src="http://thumb7.shutterstock.com/display_pic_with_logo/599431/127612211/stock-photo-green-apple-isolated-on-white-background-127612211.jpg" width=300 height=150> </div> <div class="textHolder">some text goes here</div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM