2012-04-07 6 views
0

次の関数を使用してページを読み込みます。私はリンクの負荷があり、すべてのリンクに追加することはできません。私はすべてのリンクに追加したいがないように、「INDEXjqueryを通してすべてのリンクに関数を追加するには?

var oP = document.getElementsByTagName("a"), 
    ctr = 0 
; 

while(ctr < oP.length) { 
    var oldHref = document.getElementsByTagName("a")[ctr].href; 

    document.getElementsByTagName("a")[ctr].href = "javascript:loadPage('" + oldHref + "');"; 
    ctr++; 
} 

function LoadPage(url) { 
    $("#canvas").load(url); 
} 

は、私はすべての<a>タグにhref値を取得し、ちょうど次のようにすべてのリンクには、この機能を追加する機能が欲しいです。 HTML "このような

+0

あなたが提供した通常のJavascriptコードに関する2つのコメント:1. 'document.getElementsByTagName(" a ")が' oP'に保存されています。 2.他の要素の集合の反復は、必要な反復回数を知っているので、whileループではなく、forループで一般的に行われます。 –

答えて

2

何か:

// select all links 
$('a') 
    // check that the pathname component of href doesn't end with "/index.html" 
    .filter(function() { 
    return !this.href.pathname.match(/\/index\.html$/); 
    // // or you may want to filter out "/index.html" AND "/", e.g.: 
    // return !this.href.pathname.match(/\/(index\.html)?$/i) 
    }) 
    // add a click event handler that calls LoadPage and prevents following the link 
    .click(function(e) { 
    e.preventDefault(); 
    LoadPage(this.href); 
    }); 

あなたが動的にページのセクションをロードしているので、あなたが代わりにイベントの委任を設定する必要があります。これを行う方法は、使用しているjQueryのバージョンによって異なりますが、.on()(jQuery 1.7+)または.delegate()(jQuery 1.7より前)のいずれかの関数を使用します。あなたは新しいし、@ AnthonyGristのような関数を適用するために使用できるコールバック関数のために新たにロードされたページ、$.load() takes a second argument上のリンクの変換についてあなたの質問への回答で

$('body').on('click', 'a', function(e) { 
    if(!this.href.pathname.match(/\/index\.html$/)) { 
     e.preventDefault(); 
     LoadPage(this.href); 
    } 
}); 
+0

そのページを読み込んでいますが、新しく読み込まれたページのリンクを変換しません。私は新しく読み込まれたページのために何をすべきですか? – danny

+0

には、そのスクリプトも新しく読み込まれたページに含まれています。 –

+1

@dannyコンテンツを動的に読み込んでいるので、イベントの委任が必要です。私は '.on()'を使ってコードを含めるように答えを更新しました。 - 1.7より前のバージョンのjQueryを使用している場合は、代わりに '.delegate()'を使う必要があります。構文の違いは比較的小さいので、必要に応じて、リンクされているドキュメントのセクションを読むことで変換できるはずです。 –

0

.on()の例では、このようになりますコンテンツ、例えば:

function loadPage(url) { 
    // add a callback to $.load() to be executed for the next content 
    $("#canvas").load(url, function() { convertLinks(this); }); 
} 

function convertLinks(context) { 
    // select all links in the given context 
    $('a', context) 
    // check that the pathname component of href doesn't end with "/index.html" 
    .filter(function() { 
     return !this.href.pathname.match(/\/index\.html$/); 
     // // or you may want to filter out "/index.html" AND "/", e.g.: 
     // return !this.href.pathname.match(/\/(index\.html)?$/i) 
    }) 
    // add a click event handler that calls LoadPage and prevents following the link 
    .click(function(e) { 
     e.preventDefault(); 
     loadPage(this.href); 
    }) 
    ; 
} 

// call convertLinks on the whole document on initial page load 
$(function() { convertLinks(document); }); 

.on()を使用しても同様に合理的な解決策です。

+0

@Jordenあなたのコードは私に行方不明のエラーを与えています}。再チェックしていただけますか? – danny

+0

@danny修正されました。それは3行目にあった。一般的にStack Overflowの答えのコードは、コピー&ペーストされるのではなく、一度理解すれば自分で理解して実装することを意図しています。 ;) –

+0

このエラーが発生しましたthis.href.pathname is undefined – danny

関連する問題