Gol*_*les 2 javascript jquery prepend
这种前置方式对我有用:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#learn-more-button").click(function() {
$("#main-text").load('ajax/learn-more.html #learn-more', function() {
$(this).prepend('<p> DARN </p>'); // <=== This here WORKS
$(this).hide().fadeIn(500); //Fade in
$("#main-text").animate({ // Then Animate
width:"500px",
left:'+=400px',},
400
);
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
这种前置方式对我不起作用:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#learn-more-button").click(function() {
$("#main-text").prepend('<p> DARN </p>'); // <=== This here doesn't work
$("#main-text").load('ajax/learn-more.html #learn-more', function() {
$(this).hide().fadeIn(500); //Fade in
$("#main-text").animate({ // Then Animate
width:"500px",
left:'+=400px',},
400
);
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
为什么第二种方式不起作用?怎么做才能让它发挥作用?
我不是一个真正的jQuery人,所以请帮助:)
执行load()时,#main-text元素的内容被完全替换.因此,如果您在第二个方法<p>DARN</p>之前添加load- ,它将被覆盖.
在第一种方法中,<p>它被添加到负载的回调函数中.在加载完成后调用.
您还应该将方法链接到一个选择器以获得更好的性能,如下所示:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#learn-more-button").click(function() {
$("#main-text").load('ajax/learn-more.html #learn-more', function() {
$(this).prepend('<p> DARN </p>')
.hide().fadeIn(500)
.animate({
width:"500px",
left:'+=400px'
}, 400);
});
});
});
</script>
Run Code Online (Sandbox Code Playgroud)