다양한 기사 정보가 포함 된 div가 동적으로 생성됩니다. 가장 중요한 것은 각 div에 대한 고유 한 태그 목록입니다.필터 로직 문제 jQuery
<div class="resultblock">
<h3 id="sr-title"><a href="/Code/Details/2">Blog Post</a></h3>
<p id="srdescription">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
<p>
Last Updated: 01/01/3001 00:00:00 <span class="resultright">
Author: Kayra</span></p>
<p>
Project: Kayra <span class="resultright">CMS:
Umbraco</span></p>
<p class="tag">
Tags:
<a href="/Code?SearchString=Web%20Forms">Web Forms</a> |
<a href="/Code?SearchString=Blog">Blog</a> </p>
</div>
또한 div를 필터링하기 위해 버튼으로 사용하기 위해 동적으로 생성 된 태그의 전체 목록이 있습니다. divs를 on/off 토글하고 싶습니다. 예를 들어 사용자가 Facebook 버튼을 클릭하면 Facebook div 만 표시되고 사용자가 Facebook을 다시 클릭하면 모든 div가 표시됩니다. 버튼이 누적 적으로 작동하기를 바랍니다. 사용자가 Facebook 및 MVC를 클릭하면 Facebook 및 MVC 게시물 만 표시됩니다.
<div id="tagbar">
<input type="submit" value="Facebook" class="button" /> <br />
<input type="submit" value="MVC" class="button" /> <br />
<input type="submit" value="Web Forms" class="button" /> <br />
<input type="submit" value="Blog" class="button" /> <br />
</div>
지금 내 jQuery 코드가 이상한 행동을하고 있습니다. 하나의 버튼을 클릭하면 필터가 제대로 작동하지만 필터를 클릭하여 이전과 같이 모든 게시물을 표시 할 수는 없습니다. 여러 버튼을 클릭해도 작동합니다. 때로는 버튼을 클릭하여 해제 할 수도 있지만 일관성이 없으며 때로는 여러 번 클릭해야 작동합니다.
내 코드의 논리에는 문제가 있지만 저에게 도움이되는 온라인 리소스를 찾을 수 없습니다. 죄송합니다 내 질문이 모호한 경우 있지만 정확히 내가 문제가 어디 있는지 정확히 단지 jQuery 사용하기 시작한 때문입니다.
$(document).ready(function() {
var tags = new Array();
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
$("#tagbar .button").click(function() {
var clickedTag = $(this).val(); //Gets the name of the button clicked
if (tags.indexOf(clickedTag) !== -1) {
tags.remove(tags.indexOf(clickedTag));
console.log("unclick");
}
else {
tags.push(clickedTag);
console.log("click");
}
$(".resultblock").each(function() {
var theBlock = $(this);
i = 0;
$(tags).each(function() {
var targetTags = theBlock.find(".tag a").text();
if (!theBlock.hasClass("show") && targetTags.indexOf(tags[i]) !== -1) {
$(theBlock).show();
$(theBlock).addClass("show");
}
else if (!theBlock.hasClass("show")) {
$(theBlock).hide();
};
console.log(tags[i] + " is comparing to " + targetTags + " and resulting in " + (targetTags.indexOf(tags[i]) !== -1));
i++;
});
if ($(theBlock).hasClass("show")) {
$(theBlock).removeClass("show");
}
});
});
});
JS 콘솔에 오류가 있습니까? – prodigitalson