1 回答

TA貢獻1827條經驗 獲得超9個贊
設置屬性https://api.jquery.com/attr/有attratjquery 方法。
同樣var $currQuestion = $accordionToggles.eq(i), $currAnswer = $currQuestion.closest('dt').next('dd')在這里,您將問題聲明為單個元素,并將答案作為數組,因此您必須添加eq(0)到$currQuestion.closest('dt').next('dd'),或者遍歷所有 el2 項目以設置屬性。
當您包裝dd到其他標簽divs時,jquerynext()無法在dt附近找到,因此您$currAnswer的未定義,您必須使用$currQuestion.closest('dt').next('.tab-content.tab-space').find('dd')
解決方案:
setAriaAttr = function (el, ariaType, newProperty) {
el.attr(ariaType, newProperty);
},
setAccordionAria = function (el1, el2, expanded) {
setAriaAttr(el1, 'aria-expanded', expanded);
setAriaAttr(el2, 'aria-expanded', !expanded);
},
...
for (var i = 0; i < $accordionToggles.length; i++) {
var $currQuestion = $accordionToggles.eq(i),
$currAnswer = $currQuestion.closest('dt').next('.tab-content.tab-space').find('dd').eq(0);
工作小提琴https://jsfiddle.net/w7gpLrse/
- 1 回答
- 0 關注
- 209 瀏覽
添加回答
舉報