我无法在任何地方找到这个解决方案。
用户进行选择并输入一些值。当他点击添加时,我希望使用jQuery添加的html动画(或向下滑动)到视图中。如何检索元素高度以设置值?
这是父母;
<div class="order-summary-wrap"></div>
这段代码有效,但它很不耐烦。
// build the summary boxes from the users inputs
function create_summary(){
var summary_html = '';
if( $('.cycle-01').hasClass('cycle-slide-active') ){
summary_html = '<div class="toll-free-summary-contents remove-me"><p class="line-one"><span class="new-toll-free-number">' + $('.cycle-01 .step-2-container .number').find("option:selected").attr("value") + '</span> in <span class="from-toll-free-country">' + $('.cycle-01 .step-1-container .country').find("option:selected").attr("value") + '</span> will ring to <span class="forward-to-number">' + $('.cycle-01 .step-3-container .country').find("option:selected").attr("value") + '</span> in <span class="to-toll-free-country">' + $('.cycle-01 .step-4-container #ForwardNumberTo').attr("value") + '</span></p><p class="line-two"><span class="toll-free-cost">' + $('.billing-options-hidden').find("option:selected").attr("value") + '</span> (FIRST MONTH FREE) with each minute used costing <span class="toll-free-per-minute-cost">' + $('.per-minute').text() + '</span></p><div class="remove"><a class="remove-link" href="#">Remove</a><a class="view-link" href="#">View Sample Bill</a></div></div>';
} else {
summary_html = '<div class="local-summary-contents remove-me"><p class="line-one"><span class="country-local-number">' + $('.cycle-02 .step-1-container .country').find("option:selected").attr("value") + '</span> <span class="state-local-number">' + $('.cycle-02 .step-2-container .state').find("option:selected").attr("value") + '</span> <span class="city-local-number">' + $('.cycle-02 .step-3-container .city').find("option:selected").attr("value") + '</span> <span class="new-local-number">' + $('.cycle-02 .step-4-container .local').find("option:selected").attr("value") + '</span></p><p class="line-two"><span class="toll-free-cost">' + $('.billing-options-hidden').find("option:selected").attr("value") + '</span> (FIRST MONTH FREE) with each minute used costing <span class="toll-free-per-minute-cost">' + $('.per-minute').text() + '</span></p><div class="remove"><a class="remove-link" href="#">Remove</a><a class="view-link" href="#">View Sample Bill</a></div></div>';
}
$('.order-summary-wrap').append(summary_html);
$('.add-more-numbers').removeClass('hidden');
}
// click event to dynamically add the summary boxes to the DOM
$('.first-step, .add-number').click(function(e){
create_summary();
});
此外,当点击“删除”链接时,我希望元素在从DOM中删除时进行动画处理。
// lets the user remove the number from the DOM
$('.order-summary-wrap').on('click', '.remove-link', function(e) {
e.preventDefault();
$(this).parent().parent().remove();
});
答案 0 :(得分:1)
如果你要下滑的是新的summary_html,你可以这样做。在附加DOM元素之前隐藏它,然后使用.slideDown()
使其在追加后显示。
这是一个工作演示(用简单的代码来说明):http://jsfiddle.net/jfriend00/gHcZJ/
而且,这是你实现的代码:
// build the summary boxes from the users inputs
function create_summary(){
var summary_html = '';
if( $('.cycle-01').hasClass('cycle-slide-active') ){
summary_html = $('<div class="toll-free-summary-contents remove-me"><p class="line-one"><span class="new-toll-free-number">' + $('.cycle-01 .step-2-container .number').find("option:selected").attr("value") + '</span> in <span class="from-toll-free-country">' + $('.cycle-01 .step-1-container .country').find("option:selected").attr("value") + '</span> will ring to <span class="forward-to-number">' + $('.cycle-01 .step-3-container .country').find("option:selected").attr("value") + '</span> in <span class="to-toll-free-country">' + $('.cycle-01 .step-4-container #ForwardNumberTo').attr("value") + '</span></p><p class="line-two"><span class="toll-free-cost">' + $('.billing-options-hidden').find("option:selected").attr("value") + '</span> (FIRST MONTH FREE) with each minute used costing <span class="toll-free-per-minute-cost">' + $('.per-minute').text() + '</span></p><div class="remove"><a class="remove-link" href="#">Remove</a><a class="view-link" href="#">View Sample Bill</a></div></div>');
} else {
summary_html = $('<div class="local-summary-contents remove-me"><p class="line-one"><span class="country-local-number">' + $('.cycle-02 .step-1-container .country').find("option:selected").attr("value") + '</span> <span class="state-local-number">' + $('.cycle-02 .step-2-container .state').find("option:selected").attr("value") + '</span> <span class="city-local-number">' + $('.cycle-02 .step-3-container .city').find("option:selected").attr("value") + '</span> <span class="new-local-number">' + $('.cycle-02 .step-4-container .local').find("option:selected").attr("value") + '</span></p><p class="line-two"><span class="toll-free-cost">' + $('.billing-options-hidden').find("option:selected").attr("value") + '</span> (FIRST MONTH FREE) with each minute used costing <span class="toll-free-per-minute-cost">' + $('.per-minute').text() + '</span></p><div class="remove"><a class="remove-link" href="#">Remove</a><a class="view-link" href="#">View Sample Bill</a></div></div>');
}
summary_html.hide();
$('.order-summary-wrap').append(summary_html);
summary_html.slideDown();
}
// click event to dynamically add the summary boxes to the DOM
$('.first-step, .add-number').click(function(e){
create_summary();
});
答案 1 :(得分:0)
jfriend00打败了我。你也可以这样做,从DOM中删除数字。
$('.order-summary-wrap').on('click', '.remove-link', function(e) {
e.preventDefault();
$(this).parent().parent().slideUp().remove();
});