通过自定义jQuery代码创建幻灯片库。无法在JavaScript幻灯片中嵌入标签
HTML:
<div id="backgrounds">
<img src="img/site/myimg.jpg" alt="Sample" data-title2="My Subject" data-desc="My Desc" />
<img src="img/site/myimg2.jpg" alt="Sample" data-title2="My Subject" data-desc="My Desc" />
<img src="img/site/myim3.jpg" alt="Sample" data-title2="My Subject" data-desc="My Desc" />
</div>
JS功能:
function ChangeBackground(background, direction) {
if (is_animating)
return;
is_animating = true;
$background = $(background);
$newItem = null;
if (direction == -1) {
$newItem = $background.prev();
if ($newItem.length == 0) {
$newItem = $("#backgrounds img").last();
}
} else {
$newItem = $background.next();
if ($newItem.length == 0) {
$newItem = $("#backgrounds img").first();
}
}
$background.fadeOut(300);
$newItem.fadeIn(300, function() {
$("#backgrounds img").removeClass("current");
$newItem.addClass("current");
is_animating = false;
});
$clone = $("div.item.current").clone();
$clone.removeClass("current").addClass("new");
$clone.find("h1").html($newItem.attr("alt"));
$clone.find("h2").html($newItem.attr("data-title2"));
$clone.find("p").html($newItem.attr("data-desc"));
$clone.css({ display: "none" });
$("div.item.current").after($clone);
$("div.item.current").fadeOut(300, function() {
$(this).remove();
});
$clone.fadeIn(300, function() {
$(this).removeClass("new").addClass("current");
});
}
它的工作完美。 data-desc就像幻灯片标题一样。我想要在此标题中嵌入标签。
所以:
<img src="img/site/myim3.jpg" alt="Sample" data-title2="My Subject" data-desc="My Desc <a href="#">My a tag</a> />
但不起作用。当查看DevTools的验证码时,我看到:
<img src="img/site/myimg3.jpg" alt="Sample" data-title2="My Subject" data-desc="My desc <a href=" #"="" style="width: 1451px; height: 956.0029282576867px; margin-left: -725.5px; left: 50%;">
我该如何解决它?
将它们编码为html实体,在php中,例如可以用'htmlspeciachars'来完成。 –