簡體   English   中英

如何為僅特定URL的超鏈接添加屬性?

[英]How to add attribute to hyperlink only for specific URLs?

我正在嘗試添加特定URL(知識庫)的屬性,以在新選項卡中將其打開。

我的代碼:

jQuery("a[href^='http://knowledgebase.sample.net']").attr("target","_blank");

那是我在functions.php腳本:

function target_blank_script() {
  // register your script location, dependencies and version
  wp_register_script('target_blank', get_template_directory_uri() . '/js/target_blank.js',array('jquery'), '1.0' );
  // enqueue the script
  wp_enqueue_script('target_blank');
}

add_action('wp_enqueue_scripts', 'target_blank_script');

但這是行不通的。

您的腳本失敗,因為該鏈接在jQuery腳本中運行時不存在,因為該鏈接已加載到HEAD中。 您需要讓WP在頁面底部加載腳本。

function target_blank_script() {
    wp_eneueue_script(
        'target_blank',
        get_template_directory_uri() . '/js/target_blank.js',
        array('jquery'),
       '1.0'
        true
    };
}
add_action('wp_enqueue_scripts', 'target_blank_script');

關鍵是最后一個參數(true),它告訴WP將其加載到頁腳中。 請注意,如果要立即加入腳本,則無需注冊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM