如何仅为特定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将其加载到页脚中。请注意,如果要立即将脚本排入队列,则不需要注册脚本。
相关文章:
- 将超链接添加到“;标题“;标记文本
- 如何将超链接添加到通过传单创建的传单多段线.Draw
- 将数据库中的超链接添加到查询结果
- 在CZML中的滑块内容中添加超链接
- 如何向超链接添加变量值
- 使用预先连接的事件向DOM动态添加超链接
- 将fadeIn效果添加到图像容器超链接附加字符串中
- 如何仅为特定URL向超链接添加属性
- 将带有单击事件的超链接添加到 Dojo 网格
- 如何在数组中添加超链接(Javascript)
- 将 JavaScript 添加到超链接控件
- 通过jQuery单击另一个按钮时动态地将数据添加到超链接
- 在完整日历中添加指向日期编号的超链接
- 想要在我的 GWT 应用程序 (Java 7) 中为每个 http/https 超链接添加锚 html 标记
- 如何将超链接添加到谷歌图表组织结构图
- href向超链接添加额外信息
- 向超链接添加文本输入
- 在ExtJS中为超链接添加侦听器
- 将超链接添加到谷歌图表中的自定义工具提示
- 将超链接添加到 JavaScript 图像数组