当用户选择文本时,我需要在执行最终任务之前检查两件事:
- 所选文本是否在主 .entry-content div 中? 我已经做到了。
- 所选文本是否在任何突出显示 css 类之外或疯狂? 我需要帮助。
基本上:
- 如果选择“他早熟”,则返回false
- 如果选择“第一南非 bool bool 俱乐部的负责人”,则返回真
HTML 源代码:
<p>
<span id="subject-47" class="subject highlighted">Semencic credits his early familiarity with the breed to his own travels to South Africa<span class="count">4</span></span>, but especially to his frequent correspondence with the head of the first South African Boerboel club, one Mr. Kobus Rust. <strong>The Boerboel Breeders Association was established in 1983</strong> in the Senekal district of the Free State with the sole objective of ennobling and promoting the Boerboel as a unique South African dog breed.
</p>
我当前的 Javascript(工作但我需要最后一个检查功能)
$( window ).load(function() {
$(document.body).bind('mouseup', function(e){
snapSelectionToWord();
var txt = getHTMLOfSelection();
var contentPos = $('.entry-content').html().indexOf( txt );
if( contentPos > 0 ) {
// do my thing here
// after I check txt IS NOT within a .highlighted css class
}
});
});
最佳答案
我设法按照我的意愿构建了一个函数。
function isTextInClass( text, css_class ) {
var result = false;
$( '.highlighted' ).each( function( index ) {
if( $( this ).html().indexOf( text ) != 'undefined' ) {
var foundAt;
foundAt = $( this ).html().indexOf( text );
if( foundAt > 0 ) {
result = true;
}
}
});
return result;
}
关于javascript - 检查所选文本是否在给定的 CSS 类中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37002847/