Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查所选文本是否在给定的CSS类中?_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 检查所选文本是否在给定的CSS类中?

Javascript 检查所选文本是否在给定的CSS类中?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,当用户选择文本时,在执行最终任务之前,我需要检查两件事: 所选文本是否在main.entry content div中我已经做到了。 所选文本是否在任何突出显示的css类之外我需要帮助。 基本上: 如果选择了“他的早期熟悉度”,则返回false 如果选择了“第一个南非波尔波尔俱乐部的负责人”,则返回true HTML源代码: <p> <span id="subject-47" class="subject highlighted">Semencic cre

当用户选择文本时,在执行最终任务之前,我需要检查两件事:

  • 所选文本是否在main.entry content div中我已经做到了。
  • 所选文本是否在任何突出显示的css类之外我需要帮助。
基本上:

  • 如果选择了“他的早期熟悉度”,则返回false
  • 如果选择了“第一个南非波尔波尔俱乐部的负责人”,则返回true
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>

我成功地构建了一个函数,实现了我想要的功能

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;

}

以下是简短的表格或答案。如果使用
window.getSelection().baseNode.parentNode
它将为您提供高亮显示文本的节点(外部元素)。我也会给你的id或类附加到它。基本上,如果将上述代码段的结果存储在变量中,则可以使用
id
.className
分别获取id和类

下面是一个示例代码 $(窗口)。加载(函数(){

请尝试以下代码:


如果所选文本高亮显示的父标记类返回false。

您可以使用来获取父类,然后检查它是否等于高亮显示。是否可以将此问题重新表述为更一般的问题?令人困惑的是:“文本超出或破坏任何高亮显示的css类”或“他的早期熟悉度”或“getHTMLOfSelection()同样,css类“突出显示”是围绕整个主题的,而不仅仅是“在突出显示的css类之外或疯狂”…$('.entry content')???这个类在哪里?或div?
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;

}
$(document.body).bind('mouseup', function(e){
    //gets the node(Outer HTML) of the selected txt
    var sell = window.getSelection().baseNode.parentNode;
    alert(sell);
});

}); 
$(document.body).bind('mouseup', function(e){

        //snapSelectionToWord();
        var txt = getSelectionParentElement();

        class_select = txt.getAttribute("class");

        if(class_select !== null && class_select.indexOf("highlighted") > -1) {
            alert("highlighted");
          return false;
        } else {
            alert("NO highlighted");
          return true;
        }
        /*
        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 getSelectionParentElement() {
    var parentEl = null, sel;
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.rangeCount) {
                parentEl = sel.getRangeAt(0).commonAncestorContainer;
            if (parentEl.nodeType != 1) {
                parentEl = parentEl.parentNode;
            }
        }
    } else if ( (sel = document.selection) && sel.type != "Control") {
        parentEl = sel.createRange().parentElement();
    }
    return parentEl;
}