找出所选文本是否在输入字段或textarea

时间:2015-09-24 11:37:25

标签: javascript jquery selection textselection

我正在创建Chrome扩展程序。

每当您选择文字时,我会在选区旁边的网页上添加两个按钮。

如果选择在textarea或输入框内,我不想要采取任何行动。

以下是我目前使用的代码

$(document).on("mouseup", function(e) {
    var class1=e.target.nodeName;
    var class2=e.target.firstChild;
    console.log(class1);
    console.log(class2);
    if(class1=="INPUT" || class1=="TEXTAREA"){
    }
    else{
    var selectedtext=window.getSelection().toString();
    var chars=selectedtext.length;
if(chars>2){
    var posx=e.clientX;
    var posy=e.clientY;
    var scrollDepth=$(window).scrollTop();
    posy=posy+scrollDepth;
    var location=findSelectionCoords(posx,posy);
    create_Button(location);
}
}
});

创建按钮功能用于创建按钮。

如果用户在文本区域/输入框内选择了一些文本,则此功能有效,控制台显示input / textarea。 但有时也会选择textarea或输入框,然后此功能失败。在class1中,我在这种情况下得到了DiV。

有人可以建议一个在所有情况下都能正常工作的函数,并检测文本选择是否在输入或文本区域内

提前致谢

2 个答案:

答案 0 :(得分:0)

以下是示例代码,仅当mouseup事件来自非textareainput类型文本的元素时,才会显示警告消息。

编辑代码:

$(function() {
    var isInputOrTextarea = false;
	$(document).on("mousedown", function(e) {
        // On mousedown flag whether it was clicked in input or textarea
        if($(e.target).is("textarea,input[type=text]")) {
            isInputOrTextarea = true;
        }
    });    
	$(document).on("mouseup", function(e) {
        if($(e.target).is("textarea,input[type=text]") || isInputOrTextarea) {
            isInputOrTextarea = false;
            return;
        }
        isInputOrTextarea = false;
        alert("Not in text area or input");
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" value="Lorem ipsum"/>
<textarea>
    Lorem ipsum
</textarea>
<p>
    Lorem ipsum
</p>
   

答案 1 :(得分:0)

只是在javascript中,一个如何从输入内部清除事件的示例。这是一个班轮。

#Initilaize variables

lowUS = 6.00;
medUS = 9.00;
highUS = 12.00;
lowCan = 8.00;
medCan = 12.00;
highCan = 15.00;

#Greet user and ask for input
print("Welcome to Ben's shipping calculator!");
print("We will calculate your shipping cost for you!");


orderTotal = float(input("Please input your total amount."));
country = input("In which country do you reside? Please type C for Canada or U or USA. ");

#Validate input
while country not in {'u', 'c', 'C', 'U'}:
    print ("Invalid input. Please try again. ")
    country = input("In which country do you reside? Please type C for Canada or U or USA. ");

#Determine how much the shipping fee is
if country == 'U' or country == 'u':
    if orderTotal <= 50.00:
        if orderTotal > 50.00 and orderTotal <= 100.00:
            if orderTotal > 100.00 and orderTotal <= 150.00:
                if orderTotal > 150.00:
                    print("Your shipping is free and your grand total is", orderTotal)
                else:
                    print("Your shipping fee is: ", highUS);
                    orderTotal = (orderTotal + highUS); 
            else:
                print("Your shipping fee is: ", medUS);
                orderTotal = (orderTotal + medUS);
        else:
            print("Your shipping fee is: ", lowUS);
            orderTotal = (orderTotal + lowUS);

elif country == 'c' or country == 'C':
    if orderTotal <= 50.00:
        if orderTotal > 50.00 and orderTotal <= 100.00:
            if orderTotal > 100.00 and orderTotal <= 150.00:
                if orderTotal > 150.00:
                    print("Your shipping is free and your grand total is", orderTotal)
                else:
                    print("Your shipping fee is: ", highCan);
                    orderTotal = (orderTotal + highCan); 
            else:
                print("Your shipping fee is: ", medCan);
                orderTotal = (orderTotal + medCan);
        else:
            print("Your shipping fee is: ", lowCan);
            orderTotal = (orderTotal + lowCan);

print("Your grand total is: $", orderTotal);