javascript - 如何使用js/jquery访问标签的title属性
问题描述
解决方案
您已经在 if 语句中获得了 title 的值,但是您'title'
在分配中使用了该字符串。您需要$(this).attr('title')
在两个地方都使用,或者更好地执行以下操作:
var title = $(this).attr('title');
if(title.includes("SOME")) {
location.hash = "#" + title;
location.reload();
}
由于您使用的是 WordPress,jQuery.noConflict()
可能正如 @charlietfl 在评论中提到的那样正在使用。因此,您可能需要用 替换所有$
实例jQuery
。
jQuery(window).on('hashchange', function() {
jQuery('.menu-item.menu-item-type-post_type.menu-item-object-page.current-menu-ancestor.current-menu-parent.current_page_parent.current_page_ancestor.menu-item-has-children.menu-item-top-level.menu-item-top-level-2.active-parent-item.dropdown_ul_available.current-menu-item')
.on('click', 'a', function() {
var title = jQuery(this).attr('title');
if(title.includes("SOME")) {
location.hash = "#" + title;
location.reload();
}
})
});
您也只在哈希更改时应用点击事件,您应该在每次页面加载时通过在文档准备好时添加来设置点击。请记住,如果可能的话,将下面的内容换成document
您想要检查的特定容器是理想的,但由于可用的 HTML 有限,我保持简单。
jQuery(function() {
jQuery('document').on('click', '.menu-item.menu-item-type-post_type.menu-item-object-page.current-menu-ancestor.current-menu-parent.current_page_parent.current_page_ancestor.menu-item-has-children.menu-item-top-level.menu-item-top-level-2.active-parent-item.dropdown_ul_available.current-menu-item > a', function() {
if(Boolean(window.location.href.toString().includes("apple-uebersicht")))
{
if(jQuery(this).attr('title').includes("iphone")) {
location.hash = "#" + 'title';
location.reload();
}
}
})
});
推荐阅读
- html - 如何获取已旋转 90 度的文本输入以占据 div 的全高
- firebase - 如何在firebase Flutter中将2个用户链接在一起
- php - PHP中的SQL_Injection函数改进
- sql - sql 表达式 SUM(col1 + col2) / COUNT(1) 其他解决方法
- algorithm - 带有 if-else 块的 for 循环的时间复杂度
- python - Django:将项目保存到数据库,ForeignKey 混淆
- r - LMM 的似然比检验给出的 P 值为 1?
- drupal-8 - Drupal 8:如何在自定义模块中更改块中的内容
- javascript - Javascript转
我正在尝试将我的 <c: out 值转换为 Javascript 日期,但是当我尝试它时,我不断收到无效的日期。这是我试图变成 Javascript dateTime 的字符串。
24-02-2021 17:34:27
我通过执行以下操作获得此值:
va
- java - 背景图片itextpdf 5.5