Javascript .slideDown(jQuery)不工作
这是我的jQuery代码,用于添加由div中的post生成的html。slidedown()函数似乎工作不正常。这是我的剧本:Javascript .slideDown(jQuery)不工作,javascript,jquery,Javascript,Jquery,这是我的jQuery代码,用于添加由div中的post生成的html。slidedown()函数似乎工作不正常。这是我的剧本: $(".expand").click(function(){ var button = $(this); var id = $(this).attr("eid"); var url = "/get-complete/" + id + '/'; $.ajax({ type: "GET",
$(".expand").click(function(){
var button = $(this);
var id = $(this).attr("eid");
var url = "/get-complete/" + id + '/';
$.ajax({
type: "GET",
url: url,
success: function( data ) {
var obj = $.parseJSON(data);
var lang = '';
$.each(obj, function() {
lang += this['html'] + "<br/>";
});
button.siblings('.comment-expand').slideDown('slow', function(){
button.siblings('.comment-expand').html(lang);
});
button.attr('class', 'collapse');
button.html('Collapse');
},
});
return false;
});
试试这个:
$.ajax({
type: "GET",
url: url,
// setting the dataType to json, jQuery itself parses the response
dataType: 'json',
success: function(data) {
// Hiding the element and setting it's innerHTML
// before calling slideDown()
button.siblings('.comment-expand').hide().html(function() {
// Mapping the response and concatenating `html` properties
// If the response has only one array use:
// return data[0].html + '<br>'; instead
return $.map(data, function(v) {
return v.html + '<br>';
}).join('');
}).slideDown();
button.addClass('collapse')
.removeClass('expand')
.html('Collapse');
},
});
首先设置
innerHTML
,然后调用slideDown()
(如果元素是隐藏的)。按钮。下一步是而不是按钮。兄弟姐妹('.comment expand')。innerHTML(lang);button.sides('.comment expand')。slideDown('slow')@BlackSheep元素一开始是空的,不是隐藏的是,但是使用.html()
方法,没有.innerHTML()
方法。如果元素未隐藏slideDown()
不起任何作用,请在使用.hide()
方法隐藏元素,然后再使用.slideDown()
和设置html内容。我尝试使用委托进行此操作。但是,它反而使上述内容再次出现。@Monique这是因为事件绑定到元素而不是它们的类名,当您删除类时,单击处理程序仍然绑定到元素,一个选项是使用事件委派或.one('click')
方法。好的,我使用了一个。仍然不起作用$(.collapse”).one(“click”,function(){var button=$(this);button.sides('.comment expand').slideUp('slow');button.attr('class','expand');button.html('expand');return false;});我的意思是使用.one()
作为第一个单击处理程序,$('.expand').one('click')…
。好的,这样就行了。但是,现在还有另一个问题。当我在折叠后再次单击展开按钮时,它不起作用。那么,我应该把这两个都放在代理中吗?
[{"html": "\n <div class=\"comment-count-bar\">\n</div>\n "}]
$("body").delegate(".collapse", "click", function(){
var button = $(this);
button.siblings('.comment-expand').slideUp('slow');
button.attr('class', 'expand');
button.html('Expand');
return false;
});
$.ajax({
type: "GET",
url: url,
// setting the dataType to json, jQuery itself parses the response
dataType: 'json',
success: function(data) {
// Hiding the element and setting it's innerHTML
// before calling slideDown()
button.siblings('.comment-expand').hide().html(function() {
// Mapping the response and concatenating `html` properties
// If the response has only one array use:
// return data[0].html + '<br>'; instead
return $.map(data, function(v) {
return v.html + '<br>';
}).join('');
}).slideDown();
button.addClass('collapse')
.removeClass('expand')
.html('Collapse');
},
});
$(document).on('click', '.collapse', function() {
// var button = $(this);
// ...
});