获取并将ID传递给jQuery脚本

时间:2011-08-04 16:19:02

标签: javascript jquery

我想编写一个更改页面内容但在foreach数组中的脚本。我有这个HTML:

<div id="bigleftproject">

    <p>content which will be swapped</p>

</div>


<!-- thumbnails (foreach array -->
<div class="prj_thumb" id="thumb1"> 
    <h2 class="active">project 1</h2>
    <img src="../img/thumbnail1.png" alt="thumbnail" width="100" height="50" />
</div>

<div class="prj_thumb" id="thumb2"> 
    <h2 class="active">project 2</h2>
    <img src="../img/thumbnail2.png" alt="thumbnail" width="100" height="50" />
</div>

<div class="prj_thumb" id="thumb3"> 
    <h2 class="active">project 3</h2>
    <img src="../img/thumbnail3.png" alt="thumbnail" width="100" height="50" />
</div>
<!-- end -->

我的jQuery脚本开头了:

$(document).ready(function(){
    $('.prj_thumb').click(function() {
        $('#bigleftproject').load('http://www.mysite.com/projects/small/thumb1');
    });
});

基本上,我想要实现的是jQuery脚本获取'.prj_thumb'的ID并将其传递到.load函数中URL的最后部分。

2 个答案:

答案 0 :(得分:1)

这是你要找的吗?

$(document).ready(function(){
    $('.prj_thumb').click(function() {
        $('#bigleftproject').load('http://www.mysite.com/projects/small/' + this.id);
    });
});

答案 1 :(得分:1)

样式类 -

.btn {

}

Javascript(jQuery) -

jQuery(document).ready(function() {
    jQuery(".btn").each(function(index, element) {
        alert(this.id);
    }); 
});

HTML -

<div class="btn" id="btn1"></div>
<div class="btn" id="btn2"></div>
<div class="btn" id="btn3"></div>