gpt4 book ai didi

javascript - 在 JavaScript 函数中将 CSS id 作为参数传递

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:27 25 4
gpt4 key购买 nike

我有一个显示小红球的 CSS id 和一个 JAVASCRIPT 函数。我想将 CSS id 作为参数传递给 JavaScript 函数。我看过很多教程,但无法弄清楚。以下是代码:

CSS代码

#projectile{
position: absolute;
background-color: #ff0000;
left:176px;
top: 486px;
width:10px;
height:10px;
border-radius: 8px;
z-index: 9;
}

JavaScript代码

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); }

最佳答案

假设您正在使用 jQuery 并且您希望 jQuery 对象使用 offset()方法,因为普通 DOM 节点不存在这样的方法

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);
}

关于javascript - 在 JavaScript 函数中将 CSS id 作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com