未调用辅助链接`onclick`事件

时间:2013-03-29 15:45:04

标签: jquery onclick

我有以下代码,并且我尝试在Floater.Create()对象的click上调用[data-floatertype=minimize]。我不想使用ID,只需通过DOM。仅供参考[data-floatertype=minimize]对象已附加.on('click',...),我希望它能同时执行这两项操作。

<div data-floater="true" data-minimized="false">
    <p data-floatertype="title" class="title" style="font-size: 20px;">Welcome</p>
    <a data-floatertype="minimize" href="#" class="minimize" style="right: 30px;">-</a>  
    <a href="#" data-floatertype="close" class="close">X</a>
    <div data-floatertype="content" class="content" style="display: block;">                
    <div style="display: block; text-align: center;" data-display="block">
        <h3>Floaters 101</h3>
        <hr>
        <p style="text-align:left;">You can have as many floaters open as you would like.</p>
        <p style="text-align:left;">Floaters can be minimized by using the minimize button in the top right.</p>
        <p style="text-align:left;">Floaters can also be closed using the close button in the top right.</p>
        <hr>
        <p>Try minimizing this window now.</p>
        <script type="text/javascript">
            $(this).parent().siblings('[data-floatertype=minimize]').on('click', function () {
                Floater.Create("Good job!", "floatertutorial.php", {intro: "3"});
            });
        </script>
    </div>              
</div>

我怀疑的问题是$(this).parent().siblings(...)没有返回正确的对象,或者根本没有返回任何东西。或者...... .on('click',...)没有正确绑定。在任何情况下,单击链接时都不会调用Floater.Create()方法。救命?

1 个答案:

答案 0 :(得分:1)

this不受HTML元素“范围”的影响,this指的是您使用过的window。您将需要这样做:

$('[data-floatertype=minimize]').on('click', function () {
    Floater.Create("Good job!", "floatertutorial.php", {intro: "3"});
});