I've a CSS id which shows a small red ball and a JAVASCRIPT function. I want to pass the CSS id as a parameter in the JavaScript function. I've seen a lot of tutorials but can't figure it out. Here are the codes:
CSS code
#projectile{
position: absolute;
background-color: #ff0000;
left:176px;
top: 486px;
width:10px;
height:10px;
border-radius: 8px;
z-index: 9;
}
JAVASCRIPT code
function gofish(projectile_id){
var projectile = getElementbyId(projectile_id);
start_x = projectile.offset().left;
start_y = projectile.offset().top;
function init()
{ setTimeout("gofish('projectile')", 500); }
Lets assume you're using jQuery and that you want a jQuery object to use the offset()
method as no such method exists for plain DOM nodes
function gofish(projectile_id){
var projectile = $('#' + projectile_id);
var start_x = projectile.offset().left;
var start_y = projectile.offset().top;
}
function init() {
setTimeout(function() {
gofish('projectile');
}, 500);
}
You have a few mistakes in your JavaScript.
Here is updated JavaScript code:
function gofish(projectile_id) {
var projectile = document.getElementById(projectile_id);
start_x = projectile.offsetLeft;
start_y = projectile.offsetTop;
}
function init() {
setTimeout(function () {
gofish('projectile');
}, 500);
}
init();
And here is the code in action, so far: http://jsfiddle.net/JuvDX/
This may help you little..
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(element)
{
alert(document.getElementById(element.id).value);
}
</script>
<style>
#para1
{
text-align:center;
color:red;
}
</style>
</head>
<body>
<p id="para1" onclick="myFunction(this)" name="paragraph" >Hello World!</p>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.