JQuery:显示具有特定标题的元素

发布于 2024-08-27 09:25:44 字数 256 浏览 5 评论 0原文

我试图仅显示将 title 属性传递给函数的元素:

var selectservice = function(serviceStr) {
    $j("li.project").hide();
    $j("li.project.attr('title').contains(serviceStr)").show();
};

我不太确定如何将 title 属性与传递的字符串 serviceStr 进行比较?包含似乎不起作用。

I am trying to show only elements that have the title attribute passed to the function:

var selectservice = function(serviceStr) {
    $j("li.project").hide();
    $j("li.project.attr('title').contains(serviceStr)").show();
};

I am not quite sure how to compare the title attribute to the passed string serviceStr? contains doesnt seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-09-03 09:25:44

使用属性等于选择器,如下所示:

var selectservice = function(serviceStr) {
    $j("li.project").hide();
    $j("li.project[title='" + serviceStr + "']").show();
};

请注意,属性还有其他选项同样,不仅仅是等于,例如包含、结尾为、开头为等。 参见此处查看完整列表

Use the attribute equals selector, like this:

var selectservice = function(serviceStr) {
    $j("li.project").hide();
    $j("li.project[title='" + serviceStr + "']").show();
};

Note that there are other options for attributes as well, not just equals, like contains, ends with, starts with, etc. See here for a full list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文