1

これは私のスクリプトです:

<script type="text/javascript">
    function is_numeric(n){
        return !isNaN(parseFloat(n)) && isFinite(n);
    }

    jQuery(document).ready(function(){
        // Est-ce que on doit forcer l'ouverture d'un asset en particulier ?
        var Hash = window.location.hash;
        var Triggered = false;

        if(Hash != undefined && Hash != ''){
            Hash = Hash.substring(2);

            if(is_numeric(Hash) === true && Hash > 0){
                Triggered = true;
                jQuery('.show-container[data-id="'+Hash+'"]').trigger('click');
            }
        }

        if(Triggered === false){
            jQuery('.show-container:first-child').trigger('click');
        }

        jQuery('.show-container').bind('click', function(e){
            //  On empêche l'action par défaut du lien
            e.preventDefault();

            if(ContainerEl.css('display') == 'none'){
                ContainerEl.show(0, function(){
                    SubContainerEl.slideDown(300, function(){
                        // Other code there...
                    });
                });
            } else {
                // On cache le tout
                SubContainerEl.slideUp(300, function(){
                    ContainerEl.hide(0);
                });
            }
        });
    });
</script>

そして、これはhtmlです

<h4 class="heading"><a class="show-container" data-id="1" href="javascript:void(0);">BC600 | <span id="assign_count_1">0</span> / 5</a></h4>
<div id="asset-data-1" data-ajax-loaded="false" class="row-fluid" style="display: none;">
    <div data-container="true" class="span12" style="display: none; height: 310px; overflow: auto;"></div>
</div>

<h4 class="heading"><a class="show-container" data-id="2" href="javascript:void(0);">Ultrabook HP3608 | <span id="assign_count_2">9</span> / 25</a></h4>
<div id="asset-data-2" data-ajax-loaded="false" class="row-fluid" style="display: none;">
    <div data-container="true" class="span12" style="display: none; height: 310px; overflow: auto;"></div>
</div>

が空でない場合window.location.hashは、それを読み取って情報を抽出したいのです。#a2情報はor ... のようなものになります。#a23番号はAssetID.

有効な ID が表示された場合はクリックをトリガーし、そうでない場合はデフォルトで最初の ID をトリガーしたいと考えています。

これは機能せず、コンソールにエラーは表示されません。スクリプトが十分に進んでいるかどうかを確認するためにいくつかのアラートを配置しました。はい、彼はそうしますが、トリガーは何もしません。

ありがとう。

4

2 に答える 2

4

バインドされる前にクリックをトリガーしようとしています。コードは次のようになります。

// This section orignally appeared after the triggering of the click
jQuery('.show-container').bind('click', function(e){
    // Your code
});

if(Triggered === false){
    jQuery('.show-container:first-child').trigger('click');
}

また、注意として、使用している jQuery のバージョンによっては、bind は非推奨であり、on()に置き換える必要があります。

jQuery('.show-container').on('click', function(e){
    // Your code
});
于 2012-08-15T15:18:37.300 に答える
0

問題は、ハッシュが文字列であることです。is_numeric() は、'20' や '55' のような文字列を渡しても true を返します。

文字列を数値に変換する必要があります。

これを行う方法は次のとおりです。

var Hash = 'a23';
Hash = Hash.substring(2);

var actualNumber = Number(Hash); // Here you convert the string to number

これはうまくいくはずです。

于 2012-08-15T15:17:34.147 に答える