javascript - jQuery选择器属性值空白问题
问题描述
几周前,我一直在使用此代码将滚动事件变为特定页面,并且效果很好。
但是,昨天,客户要求将属性名称从“Ouro”更改为“Ouro Envelhecido”。
第一个功能代码是这样的:
jQuery(document).ready(function($) {
$("[data-title=Ouro]").click(function() {
$('html, body').animate({
scrollTop: $(".custom-product-page").offset().top - 100
}, 1000);
})
在属性名称更改后,我们只是添加了“Envelhecido”,如下所示:
jQuery(document).ready(function($) {
$("[data-title=Ouro Envelhecido]").click(function() {
$('html, body').animate({
scrollTop: $(".custom-product-page").offset().top - 100
}, 1000);
})
并且代码停止工作。在控制台,我收到警告说 Uncaught Error: Syntax error, unrecognized expression: [data-title=Ouro Envelhecido],但是,属性名称是 100% 正确的。
图片: https : //prnt.sc/13py6mv https://prnt.sc/13py8zy
任何帮助,将不胜感激!
解决方案
您应该使用引号来包装您的数据标题值
jQuery(document).ready(function($) {
$("[data-title='Ouro Envelhecido']").click(function() {
$('html, body').animate({
scrollTop: $(".custom-product-page").offset().top - 100
}, 1000);
})
})
推荐阅读
- java - 无法写入 Firebase 实时数据库
- python - 为图像路径抓取网站(不下载图像只是获得可点击的链接),但图像 url 被解析为抓取的文本
- java - java.lang.NoSuchMethodError:没有直接方法
(Landroid/view/SurfaceView;II)V - oracle - 在 oracle 19c 中为数据库分配更多存储空间
- google-visualization - '#12768949' 不是有效的十六进制颜色谷歌地图
- tarantool - Tarantool 中 PostgreSQL 的类似 JSONB 的可索引列?
- r - 将鼠标悬停在 R 闪亮的 selectInput 上时是否可以显示信息文本?
- php - Laravel 8 中的验证
- python - 如何使用 f 字符串遍历数据框?
- reactjs - 在 React 中导入组件时出现无效的钩子调用错误