0

この jquery コードで productbox div の ID を取得する必要があります。$this.attr('id) を試しましたが、結果はオブジェクト オブジェクトまたは未定義です。前もって感謝します

Javascript:

$('body').on('mouseenter', 'div.productbox', function() 
{
    buttonDiv = $(this).children('div.roll');
    myTimeout = setTimeout(function() 
    {
        Divid = ?;
        buttonDiv.toggle();
    }, 500);
});

$('body').on('mouseleave', 'div.productbox', function() 
{
    buttonDiv.hide();
    clearTimeout(myTimeout);
});

$('body').on('hover', 'div.productbox', function() 
{
    var buttonDiv = $(this).children('div.roll1');
    buttonDiv.toggle();
});   

HTML:

<div id="40" class="productbox">
    <span class="new">Yeni</span>
    <div class="roll" style="display: none;">
        <input type="button" value="139"> Like <input type="button" value="34"> Comment
    </div>
    <div class="productpicture" onclick="rload(40)">
        <img style="margin:auto; width:180px;overflow:hidden;display:block"  src="pictures/40.jpg">
    </div>
    <div class="ps">
        <div class="productname">Giysi</div>
        <div class="companyname" onclick="go('2');">Koton</div>
        <div class="price">9.99 TL</div>
    </div>
</div>
4

3 に答える 3

1

次のように閉鎖を利用できます。

<script type="text/javascript">
    $('body').on('mouseenter', 'div.productbox', function()
    {
        var $productBox = $( this );
        buttonDiv = $productBox.children('div.roll');
        myTimeout = setTimeout(function ()
        {
            Divid = $productBox.attr('id');
            buttonDiv.toggle();
        }, 500);
    });
    $('body').on('mouseleave', 'div.productbox', function ()
    {
        buttonDiv.hide();
        clearTimeout(myTimeout);
    });
    $('body').on('hover', 'div.productbox', function ()
    {
        var buttonDiv = $(this).children('div.roll1');
        buttonDiv.toggle();
    });
</script>

<div id="40" class="productbox">
    <span class="new">Yeni</span>
    <div class="roll" style="display: none;">
        <input type="button" value="139">
        Like
        <input type="button" value="34">
        Comment
    </div>
    <div class="productpicture" onclick="rload(40)">
        <img style="margin:auto; width:180px;overflow:hidden;display:block"  src="pictures/40.jpg">
    </div>
    <div class="ps">
        <div class="productname">Giysi</div>
        <div class="companyname" onclick="go('2');">Koton</div>
        <div class="price">9.99 TL</div>
    </div>
</div>
于 2013-04-20T14:58:45.283 に答える
0

「未定義」などを取得している理由は、実際にウィンドウの ID を要求しているためです。

必要なことは、div のローカル変数を宣言し、タイムアウト内でその変数を使用して ID を取得することです。

         $('body').on('mouseenter', 'div.productbox', function() {
             var $this = $(this);
             var buttonDiv= $this.children('div.roll');
             var myTimeout = setTimeout(function() {
                 var Divid= $this.attr('id');
                 buttonDiv.toggle();
             }, 500);
         });
于 2013-04-20T15:00:39.037 に答える