[英]how do I add class of div from other page?
I want to remove class of div id from other page anchor link. 我想从其他页面锚链接中删除div id类。
firstPage.html firstPage.html
<div class="question show" id="a1">
Sample 1
</div>
<div class="question" id="a2">
Sample 2
</div>
list.html list.html
$(function () {
$("a").click(function () {
$("#a2").addClass('question show');
});
});
</script>
</head>
<body>
<a href="firstPage.html#a1">Link 1</a>
<a href="firstPage.html#a2">Link 2</a>
</body>
I want to add class addClass('question show')
to that div id which is clicked. 我想将class
addClass('question show')
到单击的div id中。
I tried here with Link1
for id=a1
我在这里尝试使用
Link1
for id=a1
But I'm failed to set class ('question show') help me to correct my code
Please check code here http://plnkr.co/edit/fzdfjdrRbcWmir5wHcJW?p=preview But I'm failed to set class ('question show') help me to correct my code
请在这里检查代码http://plnkr.co/edit/fzdfjdrRbcWmir5wHcJW?p=preview
I'm taking a different approach. 我采取了不同的方法。 I'll not add the function to list.html .
我不会将该函数添加到list.html 。 Let the page firstPage.html be called with the value.
让页面firstPage.html与值一起调用。 We will capture the anchor from firstPage.html .
我们将从firstPage.html捕获锚点。
Also, since your all divs have the class 'question'; 此外,由于你的所有div都有类'问题'; I'm ignoring that class and targeting only 'show' class.
我忽略了那个课程,只针对'show'课程。
So, load this function with your firstPage.html : 因此,使用firstPage.html加载此函数:
$(document).ready(function(){
var call = $(location).attr('href').split('#');
var ancr = $.trim(call[1]);
if(ancr === undefined || ancr == ''){
// Anchor not set, do nothing
} else {
if (!$('#'+ancr).hasClass('show')) {
$('#'+ancr).addClass('show');
}
}
});
I also assume you don't have multiple divs with same ID (which generally should not be). 我还假设您没有多个具有相同ID的div(通常不应该这样)。
I hope this will do what you need. 我希望这能满足您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.