htmlコードを変更した後、私の関数がなぜ機能しなくなったのか理解しようとしています。htmlの変更後にjquery関数を呼び出す方法
私はdivの持っている:私は要素.box
に応じて変換をスクロールすると、ここですべてが、作業まで
$(function() {
$('.box').moveIt();
});
//move elements in different speeds
$.fn.moveIt = function() {
var win = $(window);
var it = $(this);
var instances = [];
$(this).each(function(){
instances.push(new moveItItem($(this)));
});
$('.parallax').on('scroll', function() {
instances.forEach(function(inst){
var wrap = inst.el.parents('.float');
var scrol = win.scrollTop()-wrap.offset().top;
inst.update(scrol);
});
});
}
var moveItItem = function(el){
this.el = $(el);
this.speed = parseInt(this.el.attr('data-scroll-speed'));
this.direction = this.el.attr('data-direction');
};
moveItItem.prototype.update = function(scrollTop){
var pos = scrollTop/this.speed;
this.el.css('transform', 'translate'+this.direction+'(' + -pos + 'px)');
};
OK:
<div class="float">
<div class="box" data-speed="3" data-direction="X"><h1>Hola</h1></div>
<div class="box" data-speed="2" data-direction="X"><h1>chau</h1></div>
</div>
とjQueryコードを。
しかし、私はスクロールを発射した際に再び機能が壊れて見えるように表示され、私はこのメッセージを得た後に、今私は、AJAX呼び出し
//after ajax
$.ajax({
url: 'do_content.php'
}).done(function(result) {
//result = <div class="box" data-speed="3" data-direction="X"><h1>Como estas?</h1></div>
$('.float').html(result);
});
後にクラス.float
でHTMLを変更しようとしています:
Uncaught TypeError: Cannot read property 'top' of undefined
at http://localhost/ophelia/public/js/control.js?v=1487219951:197:45
at Array.forEach (native)
at HTMLDivElement.<anonymous> (http://localhost/ophelia/public/js/control.js?v=1487219951:195:13)
at HTMLDivElement.dispatch (http://localhost/ophelia/public/utilities/jquery/jquery-3.1.1.min.js:3:10315)
at HTMLDivElement.q.handle (http://localhost/ophelia/public/utilities/jquery/jquery-3.1.1.min.js:3:8342)
を
クラス.box
の要素を変更した場合にのみこのメッセージが表示されることを理解しています(h1
のみを変更しようとしましたが、ブレークしませんが速度を変更するためにすべてを変更します)
どのように機能を再開できますか?
私は$('.box').moveIt();
で再びそれを呼び出そうとしましたが、それでも私は知っている同じエラーに
を得ることが長い質問ですが、html要素がに結び付けられているため、この問題が発生した私の問題
あなたは、同様にあなたのAjaxの機能のためのコードをしてください投稿することができますか? – thesublimeobject
私の友だちを編集しました –
関数を再呼び出ししようとしたときに、 'done()'メソッドの中に、またはその外に、ajaxメソッドの直後に呼び出しを入れましたか? – thesublimeobject