如果它是一个id,如何更改jquery中的元素css属性?

时间:2022-11-25 20:10:51

So I am currently try to change a css property of an element using jquery so that it will change once the page has loaded. For some reason though when trying to add either a css style or change its current property nothing happens?

所以我现在尝试使用jquery更改元素的css属性,以便在页面加载后它将更改。出于某种原因,虽然在尝试添加css样式或更改其当前属性时没有任何反应?

What I am trying to achieve is the twitter feed at the bottom of this website: http://www.footballkit.co.uk/

我想要实现的是本网站底部的Twitter提要:http://www.footballkit.co.uk/

Here is my code:

这是我的代码:

<<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="style.css">
<script src="jquery-1.12.0.min.js"></script>
</head>

//Main Code Below:

<body>

<div id="dr">
    <h1>Hello World</h1>
</div>

<script>
$(document).ready(function(){
    $("#dr").css("top","400px");
}

</script>
</body>
</html>

2 个解决方案

#1


3  

You have a syntax error, missing ); at the end, always check your developer's console to check for javascript errors (f12 usually)

您有语法错误,缺少);最后,总是检查开发人员的控制台以检查javascript错误(通常是f12)

$(document).ready(function(){
    $("#dr").css("top","400px");
}); //Syntax error missing );

#2


1  

You have syntax error. That is not something important.

你有语法错误。这不重要。

I made a demo for you on jsfiddle

我在jsfiddle为你做了一个演示

    $(document).ready(function(){
      $("#dr").css("top","400px");
    }); 

And if top is not working, you have to check your style. Element with static position can't get position (top, left, right, bottom) DEMO with style

如果top不起作用,你必须检查你的风格。具有静态位置的元素无法获得具有样式的位置(顶部,左侧,右侧,底部)DEMO

#1


3  

You have a syntax error, missing ); at the end, always check your developer's console to check for javascript errors (f12 usually)

您有语法错误,缺少);最后,总是检查开发人员的控制台以检查javascript错误(通常是f12)

$(document).ready(function(){
    $("#dr").css("top","400px");
}); //Syntax error missing );

#2


1  

You have syntax error. That is not something important.

你有语法错误。这不重要。

I made a demo for you on jsfiddle

我在jsfiddle为你做了一个演示

    $(document).ready(function(){
      $("#dr").css("top","400px");
    }); 

And if top is not working, you have to check your style. Element with static position can't get position (top, left, right, bottom) DEMO with style

如果top不起作用,你必须检查你的风格。具有静态位置的元素无法获得具有样式的位置(顶部,左侧,右侧,底部)DEMO