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); }
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); }
Share
Improve this question
edited Mar 28, 2016 at 22:09
user5942421
asked Mar 29, 2014 at 6:48
Shihan KhanShihan Khan
2,1884 gold badges34 silver badges70 bronze badges
5
-
Do you have a
getElementbyId
function, or did you actually meandocument.getElementbyId
– adeneo Commented Mar 29, 2014 at 6:50 -
And it looks like you're using jQuery from the
offset()
function ? – adeneo Commented Mar 29, 2014 at 6:50 - Open up the browser console, you'll find it useful to viewing error messages. – jdigital Commented Mar 29, 2014 at 6:52
-
@adeneo, I tried with both
getElementbyId
anddocument.getElementbyId
but nothing worked. And yes I'm using jquery offset function. – Shihan Khan Commented Mar 29, 2014 at 6:55 - @user2611329 - I thought so, and I've posted an answer below – adeneo Commented Mar 29, 2014 at 6:59
3 Answers
Reset to default 2Lets 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.
- getElementById should have a capital 'b', and should be called against document.
- I think the offset properties you are look for are element.offsetLeft and element.offsetTop.
- You can define an anonymous function directly on setTimeout to call init(), exactly as adeneo has suggested.
- You will still need to call init() somewhere in order for it to run.
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/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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745096641a4611018.html
评论列表(0条)