在 JavaScript 函数中使用 jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4155038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:08:33  来源:igfitidea点击:

Using jQuery in a JavaScript function

javascriptjquery

提问by Wazy

function divlightbox(val)
{
    if(val)
    {
        val=val.replace( /^\s+/g, "" );
        var count_js=0;
        var big_string='';
        document.getElementById("video_lightbox").innerHTML="";
        document.getElementById("divlightbox").style.display = "block";
        $("#video_lightbox").css({"height":"430px","top":"10%","width":"480px"});

I found out that the error is in the above. My question is can't I use jQuery and traditional JavaScript at same time?I have done coding like this numerous times and never ran into a problem like this. I used to use jQuery methods like .hide()and .css()inside JavaScript functions but this time it doesn't work.

我发现错误在上面。我的问题是我不能同时使用 jQuery 和传统 JavaScript 吗?我已经做过很多次这样的编码,从来没有遇到过这样的问题。我曾经在 JavaScript 函数中使用 jQuery 方法.hide().css()但这次它不起作用。

Thanks in advance.

提前致谢。

采纳答案by Patricia

to do things like hide(); and css() you need jquery objects. you can't do them to dom elements.

做一些像隐藏(); 和 css() 你需要 jquery 对象。你不能对 dom 元素做它们。

so you could do $('#video_lightbox').html("");or

所以你可以做$('#video_lightbox').html("");

$('#video_lightbox').empty();

回答by majick

While the other answers fix the specific problems, I don't think the OP's question (in bold) is really answered here, as depending on the specific context, $may possibly not be defined as a jQuery object yet(having had this problem myself a few times now.)

虽然其他答案解决了特定问题,但我认为 OP 的问题(粗体)在这里$没有得到真正的回答,因为根据特定的上下文,可能尚未定义为 jQuery 对象(我自己也遇到过这个问题)现在好几次了。)

In which case you would need to do something like:

在这种情况下,您需要执行以下操作:

function divlightbox(val) {
    // ...
    // just use jQuery instead of $ one time
    jQuery("#video_lightbox").css({"height":"430px","top":"10%","width":"480px"});
}

OR

或者

function divlightbox(val) {
    // define the $ as jQuery for multiple uses
    jQuery(function($) {
        // ...
        $("#video_lightbox").css("height":"430px");
        $("#video_lightbox").css("top":"10%");
        $("#video_lightbox").css("width":"480px");
    }); 
}

回答by Z. Zlatev

jQuery is JavaScript so YES. Instead .innerHTML=""just use .empty(). Instead .getElementById()use $('#..')and so on.

jQuery 是 JavaScript,所以是的。而.innerHTML=""只是使用.empty(). 而是.getElementById()使用$('#..')等等。

回答by Z. Zlatev

You must provide error in javascript console.

您必须在 javascript 控制台中提供错误。

1) Do you pass a val argument to divlightbox function()? When do you call it?

1) 您是否将 val 参数传递给 divlightbox function()?你什么时候打电话?

2) why do you use the same identifier divlightboxboth for a function and for a div id? Change name to the function please, maybe the problem could be here.

2) 为什么divlightbox对函数和 div id使用相同的标识符?请将名称更改为函数,也许问题可能出在这里。

3) Always check if video_lightbox and divlightbox exist before accessing them.

3) 在访问它们之前,请务必检查 video_lightbox 和 divlightbox 是否存在。