简体   繁体   English

用jQuery编写JavaScript并使用变量

[英]Writing javascript with jquery and using a variable

As a javascript newbie, I am struggling to use a script with a variable that runs a bit of JQuery (and also struggling to use the right language here, I'm sure!) 作为一名JavaScript新手,我正在努力使用带有运行JQuery的变量的脚本(当然,在这里也很难使用正确的语言!)

The action I want to happen is to change the CSS class of a specific div, eg #det90, for which I have the following code (I have used the same on a $(window).load(function() and it works on a different set of divs): 我要执行的操作是更改特定div的CSS类,例如#det90,为此,我具有以下代码(我在$(window).load(function()上使用了相同的代码,并且在一组不同的div):

$("#MYDIVIDHERE").switchClass("sdeth","sdet",50);

So I wrote the following: 所以我写了以下内容:

<script type="text/javascript">
    $(function revealCode(divID) {
        $("#divID").switchClass("sdeth","sdet",50);
    })
</script>

and called it from an anchor with: 并从锚点调用它:

onClick="revealCode('det90');"

I think the problem is that I don't know how to write the script and pass the variable in the brackets (divID) to the next line (where I've got "#divID"). 我认为问题在于我不知道如何编写脚本并将括号(divID)中的变量传递到下一行(在该行中有“ #divID”)。 Any help or pointers to tutorials gratefully received! 任何帮助或指向教程的指点表示感谢!

Solution

Thanks to all, but particularly to Caleb. 感谢所有人,尤其是Caleb。 I've scrapped the general function and the onClick, added an ID to the anchor and inserted the following for that anchor (and then repeated that for each anchor/div combination I want to use it on ... and it works :D 我已经舍弃了常规功能和onClick,为锚添加了一个ID,并为该锚插入了以下内容(然后针对每个锚/ div组合重复此操作,我想在...上使用它,并且:D

$("#linkID").click(function() {
    $("#divID").switchClass("sdeth","sdet",50);
});

Don't quote your variable name. 不要引用变量名。 Just quote the "#" prefix. 只需引用“#”前缀即可。

<script type="text/javascript">
    $(function revealCode(divID) {
        $("#" + divID).switchClass("sdeth","sdet",50);
    })
</script>

Change to: 改成:

<script type="text/javascript">
   function revealCode(divID) {
        $("#" + divID).switchClass("sdeth","sdet",50);
    }
</script>

You don't need $() around the function 您不需要在函数周围使用$()

$("#divID") will look for an element with the ID divID , and not what was specified in your function parameter $("#divID")将查找ID为divID的元素,而不是您的函数参数中指定的元素

Change your code to: onClick="revealCode('#det90');" 将您的代码更改为: onClick="revealCode('#det90');"

$(function revealCode(selector) {
    $(selector).switchClass("sdeth","sdet",50);
})

jQuery is powered by "selectors" -- similar to CSS syntax. jQuery由“选择器”驱动-与CSS语法相似。

This won't work. 这行不通。 revealCode is local to that scope and not known outside. revealCode在该范围内是本地的,在外部是未知的。 Also, you're not using the argument you've passed into your handler. 另外,您不会使用传递给处理程序的参数。

If you're using jQuery, use it to bind to the handler as well like this: 如果您使用的是jQuery,请使用它绑定到处理程序,如下所示:

jQuery(document).ready(function() {    
    function revealCode(divID) {
        $("#" + divID).switchClass("sdeth","sdet",50);
    }

   jQuery("#divID").click(function() {
        revealCode('det90');
   });    
});

Move your onclick event handler attachment into javascript code. 将您的onclick事件处理程序附件移到javascript代码中。 You should try not to mix your functional code with your html. 您应尽量不要将功能代码与html混合使用。

Anonymous version: 匿名版本:

$('#myDiv').click(function () {
    $(this).switchClass("sdeth","sdet",50);
});

Normal version: 普通版:

var myFunction = function (element) {
    $(element).switchClass("sdeth","sdet",50);
};

$('#myDiv').click(myFunction, {element: this});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM