【问题标题】:CSS id^= selector Issue [duplicate]CSS id^=选择器问题[重复]
【发布时间】:2017-03-04 17:52:00
【问题描述】:

我有一个重复的组,其中包含一个日期字段,作为在日历中选择日期的按钮。

<button id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_1_cal_cal_img" class="btn calendarbutton" type="button">

选择器是

id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_0_cal_cal_img"
id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_1_cal_cal_img"
id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_2_cal_cal_img"

我已禁用我的日期字段,因为该值是以另一种形式生成的。

我现在不想显示那些重复的日期按钮。

我尝试了类似的方法,但它不起作用:

[id^=form_59_].fabrikForm [id^="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_"]"_cal_cal_img" { display:none; }

非常感谢您的帮助和提示。

【问题讨论】:

  • 这会起作用还是您需要指定cal_cal_img 结尾? [id^="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_"] { display:none; }
  • @Pangloss:你是对的。这似乎是一个足够接近的骗局,所以我要关闭它。但是在您看到您的评论之前我已经回答了它,我觉得我的回答除了链接线程之外确实增加了更多价值(就它可能具有的缺点而言),所以我将把我的回答也保留为 -是而不是删除它。
  • @Harry 我喜欢你的回答,已经投票了。
  • 感谢@Pangloss :) 我将按原样保留我之前的评论,以免其他人最终询问我单独回答的理由。

标签: css css-selectors


【解决方案1】:

不,没有这样的 wildcard 选择器可以帮助您匹配属性选择器中间的任何单个字符。相反,您可以尝试以下方法之一。它们不是 100% 正确,但可以让你接近你需要的东西(并且是你使用纯 CSS 所能达到的最接近的)。


演示中的第一个选择器:(开头和结尾的组合)

当元素的id 正好以文本开头 gprh_fabrik_user_registration_333_repeat___premier_rappel_co_ 时,下面演示中的第一个选择器将匹配也以准确的文本结尾 _cal_cal_img

此选择器的一个可能缺点(根据具体需要,这也将选择具有以下内容的元素 id 因为它们也以匹配值开始和结束:

id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_10_cal_cal_img"id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_100_cal_cal_img"id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_10_a_cal_cal_img"


演示中的第二个选择器:(仅使用开头,忽略结尾)

演示中的第二个选择器将选择任何元素 id 完全以文本开头gprh_fabrik_user_registration_333_repeat___premier_rappel_co_。这不关心 id 的结尾部分是什么。

这个的缺点是,它也会匹配具有如下 id 的元素:

id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co"id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_1"id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_2_cal_cal_imgs"


具有上述两个选项的演示:

[id^="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_"][id$="_cal_cal_img"] {
  background: red;
}

/* use the above one or the below depending on your need */

[id^="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_"] {
  border: 2px solid green;
}
<input id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_0_cal_cal_img" />
<input id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_1_cal_cal_img" />
<input id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_2_cal_cal_img" />
<input id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_2" />
<input id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_2_cal_img" />
<input id="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_2_cal_cal_imgs" />

【讨论】:

  • 非常感谢您的解释和时间,效果很好的解决方案是:[id^="gprh_fabrik_user_registration_333_repeat___premier_rappel_co_"][id$="_cal_cal_img"] 干杯,马克
  • 不客气@marcq。这个问题也很好,我只是在等着看我是否正确理解你,然后再投票:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 2016-01-13
  • 2012-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多