我正在尝试为我的HTML5网站实现为XHTML 1.0文档类型编写的JQuery图像库,问题是我不能在HTML5中使用自定义“rel”属性:
<!-- In <head> tag -->
<script type="text/javascript">
$(document).ready(function(){
$("a[rel='first_gallery']").colorbox({opacity: ".75"});
$("a[rel='second_gallery']").colorbox({opacity: ".75"});
});
</script>
<!-- In <body> tag -->
<a href="first_gallery/1.jpg" title="Image 1" rel="first_gallery"><img src="images/1_thumbnail.jpg" alt="Image 1 Thumbnail"/></a>
<a href="second_gallery/2.jpg" title="Image 2" rel="second_gallery"><img src="images/2_thumbnail.jpg" alt="Image 2 Thumbnail"/></a>
我尝试过使用HTML5自定义“data-rel”atrributes而不是“rel”但是我的JQuery有什么问题?
<!-- In <head> tag -->
<script type="text/javascript">
$(document).ready(function(){
$("a[data-rel='first_gallery']").colorbox({opacity: ".75"});
$("a[data-rel='second_gallery']").colorbox({opacity: ".75"});
});
</script>
<!-- In <body> tag -->
<a href="first_gallery/1.jpg" title="Image 1" data-rel="first_gallery"><img src="images/1_thumbnail.jpg" alt="Image 1 Thumbnail"/></a>
<a href="second_gallery/2.jpg" title="Image 2" data-rel="second_gallery"><img src="images/2_thumbnail.jpg" alt="Image 2 Thumbnail"/></a>
答案 0 :(得分:1)
我最近遇到了同样的问题而且我完全得到了你想要实现的目标,但首先我会修复你使用“rel”属性的初始代码“
请注意,“rel”属性是jquery.colorbox对象中的一个选项:
<!-- In <head> tag -->
<script type="text/javascript">
$(document).ready(function(){
$("a").colorbox({ rel: "first_gallery", opacity: ".75"}); // this means <a rel="first_gallery" …>
$("a").colorbox({ rel: "second_gallery", opacity: ".75"}); // this means <a rel="second_gallery" …>
});
</script>
<!-- In <body> tag -->
<a rel="first_gallery" href="first_gallery/1.jpg" title="Image 1"><img src="images/1_thumbnail.jpg" alt="Image 1 Thumbnail"/></a>
<a rel="second_gallery" href="second_gallery/2.jpg" title="Image 2"><img src="images/2_thumbnail.jpg" alt="Image 2 Thumbnail"/></a>
如您所知,上面的示例不会验证为HTML5,因为“rel”的值不会验证。
如果您想使用“data-rel”,您的代码应如下所示:
<!-- In <head> tag -->
<script type="text/javascript">
$(document).ready(function(){
$("a").colorbox({ rel: $(this).data('rel'), opacity: ".75"});
});
</script>
rel: $(this).data('rel')
告诉jquery.colorbox使用“data-rel”来包装画廊。它有些受限:你不能使用自定义的“data-”属性,也不能使用“data-rel”中的值。
上面的代码适用于使用“data-rel”的两个图库:
<!-- In <body> tag -->
<a data-rel="first_gallery" href="first_gallery/1.jpg" title="Image 1"><img src="images/1_thumbnail.jpg" alt="Image 1 Thumbnail"/></a>
<a data-rel="second_gallery" href="second_gallery/2.jpg" title="Image 2"><img src="images/2_thumbnail.jpg" alt="Image 2 Thumbnail"/></a>
如果您想为不同的画廊设置不同的选项,那么就会有不利之处,让我们说不同的不透明度,一个实际的例子就是:
<!-- In <head> tag -->
<script type="text/javascript">
$(document).ready(function(){
$("a.gallery1").colorbox({ rel: $(this).data('rel'), opacity: ".75"});
$("a.gallery2").colorbox({ rel: $(this).data('rel'), opacity: ".80"});
});
</script>
<!-- In <body> tag -->
<a class="gallery1" data-rel="first_gallery" href="first_gallery/1.jpg" title="Image 1"><img src="images/1_thumbnail.jpg" alt="Image 1 Thumbnail"/></a>
<a class="gallery2" data-rel="second_gallery" href="second_gallery/2.jpg" title="Image 2"><img src="images/2_thumbnail.jpg" alt="Image 2 Thumbnail"/></a>
我希望我没有把你的解释与你混淆。
答案 1 :(得分:0)