如何使用jquery从某个div中获取文本

时间:2012-05-14 09:11:46

标签: jquery

各位大家好,并提前感谢您阅读本文.. 我有以下HTML代码

<div id="ImagePagination" class="Pagination"> 
    Photo 
    <span id="currentPhotoCount"><#currentPhotoCount#>
    </span> of
    <span id="maxPhotoCount"><#maxPhotoCount#>
    </span>
</div>​

currentPhotoCount和maxPhotoCount变量是从jquery图像滑块脚本设置的,该脚本工作正常,但我想获取currentPhotoCount的文本,以便在另一个脚本中使用它。任何关于我如何实现这一点的建议将受到高度赞赏! 我试过这段代码..我做错了什么?

  var pic=parseInt($('#currentPhotoCount').text(), 10);
  x = '../object_images/medium/<#OBJECT_ID#>_"'+pic+'".jpg';
  $(".gallery a[rel^='prettyPhoto']").attr({src:x}); 

在我的头撞墙几个小时后,我意识到这是一个引号问题。我从x变量中删除了双引号......

      x = '../object_images/medium/<#OBJECT_ID#>_"'+pic+'".jpg';

变为

      x = '../object_images/medium/<#OBJECT_ID#>_'+pic+'.jpg';

一切都按现在的样子运行......非常感谢你帮助我!!

2 个答案:

答案 0 :(得分:3)

$('#currentPhotoCount').text()

如果你想要innerHTML:

$('#currentPhotoCount').html()

答案 1 :(得分:1)

如果您需要将文本作为字符串,只需

$('#currentPhotoCount').text()

如果你必须使用该文本作为数字进行进一步的算术运算(因为它代表当前的照片)

parseInt($('#currentPhotoCount').text(), 10);