【发布时间】:2013-08-20 00:46:14
【问题描述】:
此代码旨在能够根据其 ID 在显示和隐藏元素之间切换。
我正在尝试转换代码以使其能够轻松地为其提供标准 javascript 函数中的变量。
像这样,我可以使用这个函数来隐藏或显示一个 html 元素,只需调用该函数并给它一个 ID 作为属性。
我正在尝试这个版本的代码,但它不起作用。但是我给出了我想要更改的元素的 ID,所以有一个错误。
var x;
x=$(document);
x.ready(init);
function init(){
var x;
x=$("#titulo1"); // Here is the ID of the control element
x.click(Qocult_most("#subtitulo1"));
}
function Qocult_most(id){ //Show and scrolls down till the page's end
var x;
x=$(id); //Here is the ID element to show
x.toggle(50);
$("html, body").animate({ scrollTop: $("#STH").offset().top }, 1000);
}
而调用这个 JQuery 的 HTML 主体是:
<body>
<div id="titulo1" class="contenedor"> I'm title 1
<div id="subtitulo1"> Text to show as a sub-menu when title 1 is clicked</div>
<div id="titulo2" class="contenido"> I'm title 2
</div>
</div>
</body>
【问题讨论】:
-
Function(id) {variable = id; actions;}不是有效的 JavaScript 函数定义。你的意思是写function ID(variable, actions)? -
这只是展示我想要达到的目标的一种方式。现在我已经对问题进行了一些改进。我不太确定这种设计是否是好的方式......
-
该问题已得到纠正和改进,以符合网站规则的主题。
标签: jquery html function toggle