当使用 .html 获取 jQuery 内容时,该内容会丢失 jQuery 功能。

发布于 2024-11-26 08:31:15 字数 1323 浏览 0 评论 0原文

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-12-03 08:31:15

这个问题不是100%清楚。但是 .get() 和 .html() 返回根本不同的东西。

.get() 返回一个 DOM 元素

。 html() 返回(或设置)一个 HTML 内容字符串。

另请注意,这没有意义:

$(".photoShow").get(Photo);

除非 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:

$(".photoShow").get(Photo);

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?

淡看悲欢离合 2024-12-03 08:31:15

我认为您有些困惑:

html() 返回第一个匹配选择器的 html 内容。所以如果你有:

<div id='container'><div id='content'>Content</div></div>

$('#container').html();//returns <div id="content">Content</div> which is a string

get() 重新运行 dom 元素

<div id='container'><div id='content'>Content</div></div>

$('#container').get(0);//returns a dom object

i think you are making some confusion:

html() returns the html content of the first matched selector. so if you have:

<div id='container'><div id='content'>Content</div></div>

$('#container').html();//returns <div id="content">Content</div> which is a string

get() retruns a dom element

<div id='container'><div id='content'>Content</div></div>

$('#container').get(0);//returns a dom object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文