捕获元素jquery之外的mousedown事件

时间:2016-04-12 09:50:08

标签: javascript jquery bootstrap-popover



<div class="popover-output popover font-size-9pt" id="popover-output-div">
  <label id="lbl-output-help"></label>
  <textarea class="resize-none font-size-9pt width-400px" rows="6" id="txt-output-help" hidden></textarea>
  <div class="row font-size-8pt popover-footer">
    <a href="javascript:void(0);" id="btn-edit" onclick="BusinessPortal.Help.editHelpText();">Edit</a>
    <a class="padding-left-10px" href="javascript:void(0);" id="btn-remove" onclick="BusinessPortal.Help.removeHelpPopover();">Remove</a>
    <a href="javascript:void(0);" id="editable-save" onclick="BusinessPortal.Help.saveEditedHelpText();" hidden>Save</a>
  </div>
</div>
&#13;
$('#helpDiv').on("mousedown", function (event) {
    $('.helpBtn').popover('hide');
});
&#13;
&#13;
&#13;

我有以下代码来隐藏弹出窗口和按钮。但问题是,即使我点击弹出窗口上的按钮,它也会隐藏。

我想做的是在hoved时显示popover并使用mousedown事件隐藏popover。但是当我点击popover时它不应该隐藏。

    public class OpenandClosenewtab {

        public static void main(String[] a) throws InterruptedException {
            // Initialize driver  
            WebDriver dr = new FirefoxDriver();
            //Maximize browser window   
            dr.manage().window().maximize();
            //Go to URL  
            dr.get("http://www.google.com");
            //Set  timeout  
    dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            // Open new tab  
    dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
            //Go to URL  
            dr.get("http://www.gmail.com");
            //Set new tab timeout  
    dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            // Do some operation          
            //dr.findElement(By.id("signIn")).click();
            dr.findElement(By.id("Email")).sendKeys("abdulazeem5513");
            dr.findElement(By.id("next")).click();
            dr.findElement(By.id("Passwd")).sendKeys("azeem_#5513");
            dr.findElement(By.id("signIn")).click();
            dr.findElement(By.xpath("//span[@class='gb_1a gbii']")).click();
            dr.findElement(By.xpath("//a[@id='gb_71']")).click();
            Thread.sleep(2000);

            // Close new tab  
    dr.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");

            // Switch first tab  
            dr.switchTo().defaultContent();
            Thread.sleep(2000);
            // Write search String  
            dr.findElement(By.id("lst-ib")).sendKeys("welcome to salesforce");
            // Click on Search button          
           dr.findElement(By.xpath("//button[@class='lsb']")).click();
            Thread.sleep(2000);

            // Browser close    
            dr.close();
        }

    }

1 个答案:

答案 0 :(得分:0)

您可以使用.closest()和弹出元素选择器来检查是否在弹出窗口中单击了。如果没有,那么只隐藏弹出窗口:

$('#helpDiv').on("mousedown", function (e) {
if(!$(e.target).closest('#popover-output-div').length)
   $('.helpBtn').popover('hide');
});