使用文本框水印时 Javascript 事件不会冒泡

发布于 2024-07-15 00:44:25 字数 1030 浏览 9 评论 0原文

我正在尝试在文本框上实现一个简单的水印,当文本框获得焦点时该水印就会消失。 我发现这个 jQuery 插件看起来效果很好:

http: //remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/

水印的工作方式与此代码所宣传的一样:

<script type="text/javascript" src="/includes/_jQuery/_Library/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="/includes/_jQuery/_plugs/_hint/jquery.hint.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[title!=]").hint();
    });
</script>

<table>
    <tr>
        <td><input type="text" title="Blah" /></td>
    </tr>
</table>

但是,当向表的单击事件添加警报时,我发现当您单击文本框包含文本的部分时,该事件不会冒泡。 然而,当您单击文本框没有文本的部分时,它会很好地冒泡。 下面是我用来创建警报的 jQuery 代码:

        $("table").click(function() {
            alert("blah");
        });

有谁知道为什么当单击文本框的一部分而不是其他文本框时事件不会冒泡吗?

I'm trying to implement a simple watermark on a text box that disappears when the text box gains focus. I found this jQuery plugin that appeared to work great:

http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/

The watermark worked just as advertised with this code:

<script type="text/javascript" src="/includes/_jQuery/_Library/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="/includes/_jQuery/_plugs/_hint/jquery.hint.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("input[title!=]").hint();
    });
</script>

<table>
    <tr>
        <td><input type="text" title="Blah" /></td>
    </tr>
</table>

However, when adding an alert to the click event of the table, I found that the event doesn't bubble up when you click on the portion of the text box that has text. However it bubbles up just fine when you click on the part of the text box without text. Here is the jQuery code that I used to create the alert:

        $("table").click(function() {
            alert("blah");
        });

Does anybody have any idea why the event doesn't bubble up when part of the text box is clicked, but not the other?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2024-07-22 00:44:25

有人知道为什么单击文本框的一部分时事件不会冒泡

这似乎是 Firefox 特有的错误,影响文本输入和文本区域。 您只需几行代码即可重现它,它与 jQuery 无关,也与插件无关:(

<form onclick="alert('hello')">
    <input type="text" value="hello!" onfocus="this.value=''">
</form>

尽管我认为该插件确实存在一些相当重大的问题。它忘记在局部变量上使用“var”,让它们泄漏到全局范围。这使得同一页面上的多个“.blur”元素失败,加上“空”值仍将作为标题字符串提交到表单,并且您无法区分所输入的“真实”值。 仅当焦点

事件中输入的值发生更改时,该事件才无法冒泡。 据推测,我的猜测是 Firefox 在内部识别出 click() 发生在输入内的文本上,并期望将该事件冒泡到每个文本的祖先上。 但是,当直接父级上的单击事件导致 onfocus 触发时,该函数将从其父级中删除文本。 现在没有任何东西可以发生点击事件,没有父链可以冒泡。

Does anybody have any idea why the event doesn't bubble up when part of the text box is clicked

It seems to be a Firefox-specific bug affecting both text inputs and textareas. You can reproduce it with just a few lines of code, it's not jQuery-related or to do with the plugin:

<form onclick="alert('hello')">
    <input type="text" value="hello!" onfocus="this.value=''">
</form>

(Although I think the plugin does have some fairly significant problems. It forgets to use ‘var’ on local variables, letting them leak out to global scope. This makes multiple ‘.blur’ elements on the same page fail. Plus ‘empty’ values will still be submitted to the form as the title string, and you can't distinguish ‘real’ values typed by the user that match the title string.)

The event only fails to bubble when the input's value is changed in the focus event. Speculatively, my guess is that Firefox is internally recognising that the click() occurred on text inside the input, and expecting to bubble that event up each of the text's ancestors. But when the click event on the immediate parent causes onfocus to fire, that function is removing the text from its parent. Now there's nothing for the click event to have occurred on, no parent chain to bubble through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文