jQuery的小白這裏(不要大多數帖子開始喜歡嗎?)不串聯使用類內容的jQuery
我知道我可以用CSS單獨做到這一點然而,只是爲了讓經驗多一點的觸覺,我試圖編寫一個jQuery「spoiler」函數,如果它適當標記並在鼠標懸停中顯示,就會混淆頁面上的一些文本。
總的來說,這很好地工作 - 它用您選擇的一些文本替換文本,甚至匹配替換文本的寬度以阻止文本回流。問題出現在我在頁面上添加多個擾流類時 - 在這種情況下,它將每個擾流板中的文本連接起來,並且似乎與元素第一個匹配元素的寬度匹配。它基本上就像這個功能無法分辨每個擾流板之間的差異,所以它將它們放在一起。
我必須在相當基礎的層面上做錯事。有人能指出我的錯誤嗎?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spoiler test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
jQuery.fn.spoiler = function(spoilerReplaceText) {
$(this).addClass('spoilerCensored');
var spoilerText = $(this).text();
var spoilerWidth = $(this).width();
$(this).attr('spoilertext',spoilerText).text(spoilerReplaceText).width(spoilerWidth);
$(this).stop().hover(
function(){
var spoilerText = $(this).attr('spoilertext');
$(this).fadeOut(250,function(){
$(this).text(spoilerText).addClass('spoilerRevealed').removeClass('spoilerCensored').fadeIn(250);
});
},
function(){
$(this).fadeOut(250,function(){
$(this).text(spoilerReplaceText).removeClass('spoilerRevealed').addClass('spoilerCensored').fadeIn(250);
});
}
);
};
$('.spoiler').spoiler("don't touch this");
});
</script>
<style type="text/css">
.spoiler {display: inline-block;background-color: #000000;color: #000000;text-align: center;}
.spoilerCensored {color: #ffffff;}
.spoilerRevealed {background-color: #ffffff;color: #000000;}
</style>
</head>
<body>
<p>You can add a <span class="spoiler">spoiler to your content</span> very easily. All you need to do is <span class="spoiler">add a class called spoiler to each bit of text</span> you'd like to hide. There is a problem with <span class="spoiler">adding</span> more than one spoiler to a page though.</p>
</body>
</html>
混淆「最簡單的方法擾流文本「只是爲了使其與背景顏色相同,並在翻轉時切換。對文本本身進行加擾看起來好像過火了。 – Blazemonger 2012-02-29 15:25:56
我同意,但這種挑剔對於我來說比一個適當的商業項目更爲挑戰。我想要達到的目的不是簡單地將混淆的文本消除,而是讓用戶能夠定義顯示的文本 - 因此,這種方式效果很好。我不知道這是不是最好的方法:)我確定一個XHTML驗證器會對我使用的一些技術尖叫我! – Doug 2012-03-09 13:53:43