【问题标题】:Improve JQuery function for make it able to accept variables [closed]改进 JQuery 函数以使其能够接受变量 [关闭]
【发布时间】: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


【解决方案1】:

这样的?

function init(){
  var x = $("#check");  // Here is the ID of the control element
  x.click(
    function() {
      Qocult('#most');
    }
  );
}

function Qocult(id) {  //Show and scrolls down till the page's end
  var x = $(id); 
  x.toggle(500);
  $("html, body").animate(
    { scrollTop: $("#STH").offset().top }, 1000
  );
}

$(document).ready(init);

【讨论】:

  • 我已使用此架构使其工作,但仍然无法正常工作。我还在努力。欢迎任何帮助。
  • 不太一样 - 我正在传递一个函数,单击该函数将调用Qocult。你实际上是在init中调用Qocult
  • 太难了。我会尝试找到另一个解决方案。也许闪光。在 JQuery 中似乎不可能做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
相关资源
最近更新 更多