小弟我遇到个问题
这个算是个JQ的相册 <script type="text/javascript">
$(document).ready(function(){
$("a[rel='example1']").colorbox();
});
</script>html是这个<p><a href="ohoopee1.jpg" rel="example1" title="Me and my grandfather on the Ohoopee.">Grouped Photo 1</a></p>
<div style="display:none">
<p><a href="ohoopee2.jpg" rel="example1" title="On the Ohoopee as a child">Grouped Photo 2</a></p>
<p><a href="ohoopee3.jpg" rel="example1" title="On the Ohoopee as an adult">Grouped Photo 3</a></p>
</div>凡是有rel="example1"的属性的链接,都能以相册的形式显示现在的问题是我有两个导航,水平的和垂直的,两个地方都是第一张图片的<a></a>,其他的放到display:none的div里,这样就有两个第一张图片了,我想用JQ在鼠标mouseover的时候给<a></a>加上rel="example1"代码是$("#abc").mouseover(function () {
$("#abc").attr("rel", "example1");
});应该可以加上rel属性,但是没有效果,该怎么解决??
这个算是个JQ的相册 <script type="text/javascript">
$(document).ready(function(){
$("a[rel='example1']").colorbox();
});
</script>html是这个<p><a href="ohoopee1.jpg" rel="example1" title="Me and my grandfather on the Ohoopee.">Grouped Photo 1</a></p>
<div style="display:none">
<p><a href="ohoopee2.jpg" rel="example1" title="On the Ohoopee as a child">Grouped Photo 2</a></p>
<p><a href="ohoopee3.jpg" rel="example1" title="On the Ohoopee as an adult">Grouped Photo 3</a></p>
</div>凡是有rel="example1"的属性的链接,都能以相册的形式显示现在的问题是我有两个导航,水平的和垂直的,两个地方都是第一张图片的<a></a>,其他的放到display:none的div里,这样就有两个第一张图片了,我想用JQ在鼠标mouseover的时候给<a></a>加上rel="example1"代码是$("#abc").mouseover(function () {
$("#abc").attr("rel", "example1");
});应该可以加上rel属性,但是没有效果,该怎么解决??
<a id="test">123</a>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script language="JavaScript">
$('#test').attr('rel','123')
</script>
可以啊,是不是dom没加载上?
<html>
<head>
<title>测试</title>
<link media="screen" rel="stylesheet" href="colorbox.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript" src="../colorbox/jquery.colorbox.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("a[rel='example1']").colorbox();
$("#abc").mouseover(function () {
$("#abc").attr("rel", "example1");
});
});
</script>
</head>
<body>
<p><a href="../content/ohoopee1.jpg" id="abc" class="a1" rel="111" title="Me and my grandfather on the Ohoopee.">Grouped Photo 1</a></p>
<div style="display:none;">
<p><a href="../content/ohoopee2.jpg" rel="example1" title="On the Ohoopee as a child">Grouped Photo 2</a></p>
<p><a href="../content/ohoopee3.jpg" rel="example1" title="On the Ohoopee as an adult">Grouped Photo 3</a></p>
</div>
</body>
</html>
直接写上就有效果
请问jq加属性和直接写有什么区别吗?
$("#abc").attr("rel", "example1");
});
楼主这样试试,,因为有些a是之后动态赋予的rel属性,得加上live
加一个$("#abc").click(function () {
$("a[rel='example1']").colorbox();
});现在jq是这样的<script type="text/javascript">
$(document).ready(function () {
$("#abc").click(function () {
$("a[rel='example1']").colorbox();
});
$("a[rel='example1']").colorbox();
$("#abc").mouseover(function () {
$("#abc").attr("rel", "example1");
});
});
</script>谢谢各位
$("a[rel='example1']").colorbox();
$("#abc").mouseover(function () {
$("#abc").attr("rel", "example1");
});
});之所以动态添加的rel没反应,是因为页面加载时只加载了rel有值的元素,至于之后动态添加的rel,它就不管了。
这样是用在普通事件中。对于 $("a[rel='example1']").colorbox(); 这样的话,也许得改colorbox()这个了