gpt4 book ai didi

javascript - 将类添加到具有数组 id 的元素

转载 作者:行者123 更新时间:2023-11-30 08:12:54 27 4
gpt4 key购买 nike

我有一个包含来自 cookie 的值的数组,如果这些值之一与我想添加类测试的 h2 的 id 匹配。这是我尝试过的最新路线。谢谢

jQuery.each(arr, function(index, value) {
$("h2.#" + value).addClass("testing");
});

完整代码:

<script type="text/javascript">
$(document).ready(function(){
var cookieName = $("body").attr("id");
storedCookieName = $.cookie(cookieName);

if (storedCookieName != null) {

var cookieValues = storedCookieName;
var arr = storedCookieName.split(',');
alert( "the cookie values for this page are :" + arr);

jQuery.each(arr, function(index, value) {
$("#" + value).addClass("testing");
});


} else {
var cookieValues = '';
alert("I don't have a cookie for this page");
};

//add id's to each drop down box trigger
$('h2.contentTrigger').attr('id', function(i, value) {
return "dropDownTrigger" + (i+1);
});

//Hide (Collapse) the toggle containers on load
$(".toggle_content_container").hide();

//Switch the "Open" and "Close" state per click then slide up/down (depending on open/close state)
$("h2.contentTrigger").click(function(){
$(this).toggleClass("active").next().slideToggle("fast");
return false; //Prevent the browser jump to the link anchor
});


$("h2.contentTrigger").click(function(){

//get class of clicked item to check if dropdown is active when clicked
var triggerClass = $(this).attr("class");
//get id of clicked dropdown
var targetLink = $(this).attr("id");

if ((triggerClass == 'contentTrigger noprint active') || (triggerClass == 'contentTrigger print active')) {
cookieValues+=($(this).attr("id")+",");
$.cookie(cookieName, cookieValues, { path: '/', expires: 10 });
//alert("adding value" + cookieValues);
}else{
cookieValues = cookieValues.replace(targetLink+",", "");
$.cookie(cookieName, cookieValues, { path: '/', expires: 10 });
//alert("new value" + cookieValues);
};
});

});//end:$(document).ready
</script>

</head>
<body id="<CFOUTPUT>#SMPPageVariables.PageMetaKeywords#</CFOUTPUT>">
<div id="container">
<CFINCLUDE TEMPLATE="../libraryelements/AR_TwoDeepLeft_Navigation.element">
<div id="content">
<CFMODULE TEMPLATE="../../modules/mod_page_item_area_display.cfm" AreaID="108" PageID="#SMPPageVariables.PageID#">
<CFMODULE TEMPLATE="../../modules/mod_page_item_area_display.cfm" AreaID="109" PageID="#SMPPageVariables.PageID#">
<!--Sample of output
<div class="tab">
<h2 class="contentTrigger noprint"><a>What Is Important To Know</a></h2>
<div class="toggle_content_container noprint">
<div class="block">
<h4>Info</h4>
<p>Info:</p>
<ul>
<li>Words</li>
<li>Words</li>
</ul>
<h4>Heading</h4>
<p>Paragraph</p>
<p>Paragraph</p>
</div>
</div>
</div>
-->
</div>
<div class="hidden"><CFMODULE TEMPLATE="../../modules/mod_page_item_area_display.cfm" AreaID="110" PageID="#SMPPageVariables.PageID#"></div>
</div>
</body>
</html>

最佳答案

您的 . 不正确,使用 id 时不需要 h2。你想要这个:

jQuery.each(arr, function(index, value) {
$("#" + value).addClass("testing");
});

关于javascript - 将类添加到具有数组 id 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7916678/

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