首页 > 解决方案 > 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

任何帮助,将不胜感激!

标签: javascriptjqueryscrollattributes

解决方案


您应该使用引号来包装您的数据标题值

jQuery(document).ready(function($) {
    $("[data-title='Ouro Envelhecido']").click(function() {
        $('html, body').animate({
            scrollTop: $(".custom-product-page").offset().top - 100
        }, 1000);
    }) 
})

推荐阅读