【问题标题】:Remove/replace space from value of div ID从 div ID 的值中删除/替换空格
【发布时间】:2021-12-30 00:05:16
【问题描述】:

我有一个制造商目录页面,每个制造商 div ID 都由品牌名称动态填充。目标是允许制造商与客户分享他们的链接(例如:https://libraries.specifiglobal.com/partners/request-foodservice-equipment-libraries/#angel-po

显然这并不完美,因为名称通常包含空格和/或撇号。我想要实现的功能是理想地用破折号替换空格并完全删除撇号。

我尝试了下面的代码,但是它将所有 div ID 更改为列表中的第一个制造商。

var myText = jQuery('.card-header').closest(".w-100").attr("id");
var newMyText = myText.replace(/ /g,'-');

// remove older id and place new id
jQuery('.card-header').closest(".w-100").attr("id", newMyText);

【问题讨论】:

    标签: javascript html jquery wordpress


    【解决方案1】:
    jQuery('.card-header')
        .closest(".w-100")
        .each(function() {
            e = jQuery(this);
            e.attr('id', e.attr('id').replace(/ /g,'-'));
        });
    

    【讨论】:

    • 谢谢你的工作。我该如何替换 /./ 和 / - / 呢?这是我尝试过的。 jQuery('.card-header') .closest(".w-100") .each(function() { e = jQuery(this); e.attr('id', e.attr('id').replace(/ /g,'-').replace(/ - /g,'-').replace(/./g,'')); });
    • @jl312 试试.replace(/[.-\s]/g, '')
    猜你喜欢
    • 2019-03-08
    • 1970-01-01
    • 2013-09-25
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    相关资源
    最近更新 更多