![](/img/trans.png)
[英]How to call a JavaScript function on a web page rendered by Electron?
[英]How to make all links on web page call javascript function?
我通過AJAX在我的網站上加載了不同類型的內容。
我有一個javascript函數,它將根據URL決定是在iframe中還是在新標簽頁中打開鏈接。 但是問題是如何使所有鏈接都調用該javascript函數?
我試過了:
<base target= processurl()/>
有任何想法嗎?
謝謝
編輯:JQUERY方法效果很好。 但這僅適用於一個鏈接嗎? 當我第一次單擊鏈接時,它會正確調用該函數。 但是之后單擊的任何其他鏈接都沒有任何反應。
使用jQuery <1.7:
$( 'a' ).live( 'click', function( ev ){
ev.preventDefault(); // stop normal click on link
// do stuff
} );
請注意,jQuery 1.7更喜歡使用.on
而不是.live
:
$( document ).on( 'click', 'a', function( ev ){
....
} );
有(至少)兩種方法可以解決此問題(取決於它與Ajax代碼的適應性):
target
屬性。 所以第一種方式:
$(document).on("click", "a", function(e) {
// stop default navigation
e.preventDefault();
// 'this' is the particular 'a' that was clicked
var url = this.href;
// process url somehow as desired to decide whether to open it
// in new window, or iframe or to replace current page
});
注意到.on()
方法適用於從jQuery的版本> = 1.7,所以對老年人的jQuery使用.delegate()
方法或真的老了jQuery的使用.live()
方法 。
第二種方式(假設鏈接在頁面首次加載時全部存在):
$(document).ready(function() {
$("a").each(function() {
var url = this.href;
// process url somehow and set the target attribute as appropriate
// e.g. for a new window
this.target = "_blank";
// or for an iframe
this.target = "nameofiframe"
// etc.
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.