gpt4 book ai didi

javascript - 如果 ID 为数字,Bootstrap Accordion 不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 06:14:07 25 4
gpt4 key购买 nike

将Bootstrap 升级到4.2.1 版本后出现问题。在此版本中,当我在 accordion 中使用 id="#12" 时,它不起作用。在以前的版本中,它可以与 id="#12" 配合使用。有什么想法吗?

  <div id="accordion">
<div class="card">
<div class="card-header" id="headingOne">
<h5 class="mb-0">
<button class="btn btn-link" data-toggle="collapse" data-target="#12"
aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</button>
</h5>
</div>
<div id="12" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
<div class="card-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry
richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard
dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf
moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla
assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore
wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher
vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic
synth nesciunt you probably haven't heard of them accusamus labore
sustainable VHS.
</div>
</div>

最佳答案

虽然在 HTML 5 中,id 以数字开头是有效的,但 CSS 不允许前导数字。

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier “B&W?” may be written as “B\&W\?” or “B\26 W\3F”.

在内部,Bootstrap 4 正在使用

getSelectorFromElement(element) {
let selector = element.getAttribute('data-target')

if (!selector || selector === '#') {
const hrefAttr = element.getAttribute('href')
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : ''
}

try {
return document.querySelector(selector) ? selector : null
} catch (err) {
return null
}
}

查找您的元素。 querySelector 需要一个有效的 CSS 选择器

A DOMString containing one or more selectors to match. This string must be a valid CSS selector string; if it isn't, a SYNTAX_ERR exception is thrown.

我建议只使用有效的 CSS 标识符。您还可以使用一些技巧来逃避,但除非万不得已,否则只需使用不同的选择器

关于javascript - 如果 ID 为数字,Bootstrap Accordion 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54311361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com