質問

ID属性を変更しようとする以前の試みに触れました。リクエスト=== 0の場合、以下のコンテンツを自動ロードする方法はありますか?クリック機能を持つのではなく。

<script language="javascript">
var requests = 10;

function request(self)
{if(self.href != "#")
requests -= 1;

if(requests === 0)

var pageid = function () {
    var thisID = null;
    $('#step').click(function () {
        thisID = this.id;
        $('#content').animate({
            height: getPageHeight()
        },
        700, function () {
            $('#next-page').fadeIn(500);
            $('#content-' + thisID).fadeIn(500, function () {});
        });
        return false;
    });
};
$(window).load(pageid);
function getPageHeight() {
    var windowHeight;
    if (self.innerHeight) windowHeight = self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
    else if (document.body) windowHeight = document.body.clientHeight;
    return windowHeight;
}}
</script>
役に立ちましたか?

解決

次のコードをすべてラップしてみてください:

$(document).ready(function() { 
    // your code
}

ページが読み込まれたときにコードが実行されると、探しているコンテンツを使用する準備が整っていない(そしてまだダウンロードされていない)状況に陥ります。そのため、コードを実行する前に、ページがロードされるまで待ってください。

編集

&quot; $(window).load(pageid);&quot;は間違ったコードですが、それでもうまくいかない場合は、目標をより具体的にする必要があります。

次のようなもの:

<script language="javascript">
var requests = 10;

$(document).ready(function() { 
  // Your code here
  // $(window).load(pageid);
});

function request(self)
{if(self.href != "#")
requests -= 1;

if(requests === 0)

var pageid = function () {
    var thisID = null;
    $('#step').click(function () {
        thisID = this.id;
        $('#content').animate({
            height: getPageHeight()
        },
        700, function () {
            $('#next-page').fadeIn(500);
            $('#content-' + thisID).fadeIn(500, function () {});
        });
        return false;
    });
};

function getPageHeight() {
    var windowHeight;
    if (self.innerHeight) windowHeight = self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
    else if (document.body) windowHeight = document.body.clientHeight;
    return windowHeight;
}}
</script>

他のヒント

次のようにロードイベントで匿名関数を使用してみてください:

$(window).load(function() {
   if(requests === 0) 
      pageid(); // or whatever you need
});

ただし、最初にコードから if(requests === 0)条件を削除します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top