我有这样的代码我的网页上:

<div class="item item_wall id1"></div>
<div class="item item_wall id2"></div>
<div class="item item_wall id3"></div>
<div class="item item_wall id4"></div>
<div class="item item_wall id5"></div>

我想在jQuery的东西,这样点击与类“项目”的一个div时,它会加载从页面另一个div页面的文件名取决于DIV点击。优选地,我将它想负载:

  

something.php TYPE?= item_wall&ID = ID1

对于未来的参考,这是我结束了:

<div id="itemstable" class="item_love">
    <div class="id1"></div>
    <div class="id2"></div>
    <div class="id3"></div>
    <div class="id4"></div>
    <div class="id5"></div>
    <div class="id6"></div>
</div>

jQuery的:

<script>
$("#itemstable div").click(function(){
    var url = "something.php?type=" + $(this).parent().attr("class") + "id=" + $(this).attr("class");
    alert(url);
}); 
</script>
有帮助吗?

解决方案

呼应斯宾塞Ruport的回答,您可以让这个不是阶级使用jQuery使用自定义的时候更容易属性等。

例如,你可能DIV是这样的:

<div class="item" type="item_wall" id="1"></div>

可以很容易地创建jQuery选择和事件如下所示:

$(".item").click(function() {
    var url = "something.php?type=" + $(this).attr("type");
    windows.location = url;
});

当然,不要忘记检查的有效性,从不信任用户输入(你的HTML可能会被恶意用户改写了)。

其他提示

有关XHTML有效证件,最简单的就是你的主要DIV下添加看不见孩子DIV。例如:

<div class="item">
  <div class="variable type">item_wall</div>
  <div class="variable id">1</div>
</div>

您可以找到使用.find“自定义属性”()。 E.g:

$(".item").click(function() {
    var type = $(this).find(".type").val();
    var url = "something.php?type=" + type;
    windows.location = url;
});

意见的比特:用UI混合数据应避免

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top