I am going to use on of the tooltip plugins listed in this question: jquery tooltip, but on click instead 开发者_运维问答of hover
How would I also setup a tooltip that randomized the text shown. For instance if you click a link you could be shown one of three possible messages:
Message 1 Message 2 Message 3
ideas?
You usually have an array of messages and then generate a random index onclick.
something like
var messageArray = ["message 1", "message 2", "message 3"];
var randomNum = Math.floor(Math.random()*messageArray.size);
var myMessage = messageArray[randomNum];
or something like that. refactor to use ajax/your db if you need to
var messages = ['Message 1', 'Message 2', 'Message 3'];
$('.withTooltip').click(function(){
$("#tooltip").text(messages[Math.round(Math.random() * 3)]).show();
});
Using a very small function from PHP.js: http://phpjs.org/functions/rand:498
I do it like so:
var messages = [
'Message 1',
'Message 2',
'Message 3'
];
var random_number = rand(0, messages.length);
$("#tooltip").text(messages[random_number]);
// PHP.js rand() function.
// http://kevin.vanzonneveld.net
// + original by: Leslie Hoare
// + bugfixed by: Onno Marsman
function rand (min, max) {
var argc = arguments.length;
if (argc === 0) {
min = 0;
max = 2147483647;
} else if (argc === 1) {
throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
}
return Math.floor(Math.random() * (max - min + 1)) + min;
}
精彩评论