当使用 .html 获取 jQuery 内容时,该内容会丢失 jQuery 功能。
<div id="mainContent">
<div class="photoShow"></div>
<div class="photo_nav"></div>
<div class="photo_caption">
<div class="photo_caption_content"></div>
</div>
// .photo_content has jQuery content and photo background
<div id="content">
<div class="photo_content"></div>
<div class="photo_content"></div>
<div class="photo_content"></div>
</div>
<script>
$('.photo_nav a.photo_nav_item').click(function(){
var navClicked = $(this).index();
var Photo = $('.photo_content').get(navClicked);
var curPhoto = $(Photo).html();
$(".photo_nav a.photo_nav_item").removeClass('current');
$(".photoShow").removeClass('current');
$(this).addClass('selected');
$(".photoShow").fadeIn('slow', 'swing');
$(".photoShow").html(curPhoto);
});
</script>
// .photo_content has jQuery properties which runs fine by itself. But with this click //function, it loses the properties. Using .get, instead of .html i.e.
$(".photoShow").get(Photo);
//keeps the contents working but then I lose navigation index property.
//in conclusion, with .html, it displays correct .photo_content but DOM does not work.
//With .get, I get .photo_content that works but indexing does not work.
<div id="mainContent">
<div class="photoShow"></div>
<div class="photo_nav"></div>
<div class="photo_caption">
<div class="photo_caption_content"></div>
</div>
// .photo_content has jQuery content and photo background
<div id="content">
<div class="photo_content"></div>
<div class="photo_content"></div>
<div class="photo_content"></div>
</div>
<script>
$('.photo_nav a.photo_nav_item').click(function(){
var navClicked = $(this).index();
var Photo = $('.photo_content').get(navClicked);
var curPhoto = $(Photo).html();
$(".photo_nav a.photo_nav_item").removeClass('current');
$(".photoShow").removeClass('current');
$(this).addClass('selected');
$(".photoShow").fadeIn('slow', 'swing');
$(".photoShow").html(curPhoto);
});
</script>
// .photo_content has jQuery properties which runs fine by itself. But with this click //function, it loses the properties. Using .get, instead of .html i.e.
$(".photoShow").get(Photo);
//keeps the contents working but then I lose navigation index property.
//in conclusion, with .html, it displays correct .photo_content but DOM does not work.
//With .get, I get .photo_content that works but indexing does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题不是100%清楚。但是 .get() 和 .html() 返回根本不同的东西。
.get() 返回一个 DOM 元素
。 html() 返回(或设置)一个 HTML 内容字符串。
另请注意,这没有意义:
除非 Photo 是整数,但根据您的代码,它不是整数。
这有帮助吗?如果没有 - 您发布的代码中的哪一行没有达到您的预期?
Not 100% clear on the question. But .get() and .html() return fundamentally different things.
.get() returns a DOM element
.html() returns (or sets) a string of HTML content.
Also note that this would not make sense:
Unless Photo was an integer, which according to your code it isn't.
Does that help? If not - which line of your posted code isn't doing what you expect?
我认为您有些困惑:
html() 返回第一个匹配选择器的 html 内容。所以如果你有:
get() 重新运行 dom 元素
i think you are making some confusion:
html() returns the html content of the first matched selector. so if you have:
get() retruns a dom element