admin管理员组文章数量:1328032
I'm trying to have a click event where the user clicks a Div Question, then Jquery clones the Div Answer and displays it in a separate Div Clone.
Example here: /
For some reason the following variable is ing back null. Any ideas?
var answer = $(this).parent().find(".faq-answer").clone();
Full code:
$(document).ready(function () {
var faqQuestion = $('.faq-question');
var faqClone = $('.faq-clone');
faqQuestion.click(function () {
showAnswer();
});
faqClone.click(function () {
hideAnswer();
});
function showAnswer() {
$(".faq-clone").hide("slide");
$('.faq-clone').html("");
var answer = $(this).parent().find(".faq-answer").clone();
$('.faq-clone').append(answer.html());
$(".faq-clone").show("slide");
}
function hideAnswer() {
$(".faq-clone").hide("slide");
$('.faq-clone').html("");
}
});
I'm trying to have a click event where the user clicks a Div Question, then Jquery clones the Div Answer and displays it in a separate Div Clone.
Example here: http://jsfiddle/jessikwa/zNL63/2/
For some reason the following variable is ing back null. Any ideas?
var answer = $(this).parent().find(".faq-answer").clone();
Full code:
$(document).ready(function () {
var faqQuestion = $('.faq-question');
var faqClone = $('.faq-clone');
faqQuestion.click(function () {
showAnswer();
});
faqClone.click(function () {
hideAnswer();
});
function showAnswer() {
$(".faq-clone").hide("slide");
$('.faq-clone').html("");
var answer = $(this).parent().find(".faq-answer").clone();
$('.faq-clone').append(answer.html());
$(".faq-clone").show("slide");
}
function hideAnswer() {
$(".faq-clone").hide("slide");
$('.faq-clone').html("");
}
});
Share
Improve this question
edited Aug 4, 2014 at 19:42
ajp15243
7,9601 gold badge35 silver badges38 bronze badges
asked Aug 4, 2014 at 19:40
jessikwajessikwa
7501 gold badge8 silver badges24 bronze badges
3
- 4 The referred element (this) is not being passed over to the functions. jsfiddle/zNL63/4 – emerson.marini Commented Aug 4, 2014 at 19:42
- Where is the point to clone an hidden element and show the clone ? jsfiddle/zNL63/11 – G-Cyrillus Commented Aug 4, 2014 at 20:10
- The Question/Answers are ing from a XML document through a VB loop. The desired effect is for the popup answer to be able to happen outside of the container holding the answer/question divs, so it seemed to be best to clone it into a separate div outside this container. – jessikwa Commented Aug 4, 2014 at 20:22
3 Answers
Reset to default 7The easiest way to solve this would be to pass the handlers by reference:
faqQuestion.click(showAnswer);
faqClone.click(hideAnswer);
Now this
inside of showAnswer
and hideAnswer
will be the clicked element.
You can not access element by $(this) within a function. You would need to pass that as a parameter.
Try:
function showAnswer(passedObject) {
$(".faq-clone").hide("slide");
$('.faq-clone').html("");
var answer = passedObject.parent().find(".faq-answer").clone();
$('.faq-clone').append(answer.html());
$(".faq-clone").show("slide");
}
and then you would use that function: showAnswer($(this))
or more logical & cleaner solution is what @Kevin B suggested.
Make it even simplier, use next()
jQuery function
Is there any reason why you want to clone an hidden element and only show its clone ?
DEMO
$(document).ready(function () {
var faqQuestion = $('.faq-question');
var faqClone = $('.faq-answer');
faqQuestion.click(showAnswer);
faqClone.click(hideAnswer);
function showAnswer() {
$(this).next('.faq-answer').show('slide');
}
function hideAnswer() {
$(this).hide("slide");
}
});
and apply to .faq-answer
the .faqClone
CSS
You could even produce short answer from a data-attribute :) to shorten even more HTML .
本文标签: javascript(this)parent()find(39classname39) not workingStack Overflow
版权声明:本文标题:javascript - (this).parent().find('.classname') not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742236989a2438260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论