我一直在尝试添加带有功能的内容。当我点击锚点时应该添加具有相同锚点ID的内容,我认为这个逻辑是正确的,但没有任何事情发生。有没有更好的方法来解决这个问题。来自ID的呼叫功能
$('.tab a').click(function(){
var tabAnchor = $(this).attr('href');
if($(tabAnchor)=='#info'){
callInfo();
}
else if($(tabAnchor)=='#category'){
callCategory();
}
})
function callInfo(){
$('#info').append('<p>Info has been added</p>')
}
function callCategory(){
$('#info').append('<p>Category has been added</p>')
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="tab">
<li><a href=#info>info</a></li>
<li><a href=#category>category</a></li>
</ul>
<div id="info">
</div>
<div id="category">
</div>
使用html()而不是追加 –
'if($(tabAnchor)=='#info')'?你正在比较一个字符串的jQuery对象... – Andreas