Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

javascript - 从jQuery 1迁移到jQuery 3(Migrating from jQuery 1 to jQuery 3)

I bought an HTML template only to find it uses jQuery 1. I am trying to upgrade to jQuery 3 but I have an issue with this piece of code in the template's main.js file:(我买了一个HTML模板,只是发现它使用jQuery1。我正在尝试升级到jQuery 3,但是模板的main.js文件中的这段代码存在问题:)

var background_image = function() {
    $("[data-bg-img]").each(function() {
        var attr = $(this).attr('data-bg-img');
        if (typeof attr !== typeof undefined && attr !== false && attr !== "") {
            $(this).css('background-image', 'url('+attr+')');
        }
    });  
};

The HTML part this references is:(此引用的HTML部分是:)

 <figure class="deal-thumbnail embed-responsive embed-responsive-16by9" data-bg-img="'.$dealimage.'">

This stopped working when upgrading to jQuery 3.4.1.(升级到jQuery 3.4.1时,此操作停止了。)

Not sure what to do.(不知道该怎么办。) I would appreciate any help I can get.(我将不胜感激。)

CSS:(CSS:)

*[data-bg-img] {
    background-position: center;
    background-repeat: no-repeat;
    -webkit-background-size: cover;
   -moz-background-size: cover;
   -o-background-size: cover;
   background-size: cover;
 }
  ask by Bruno translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It turns out I overlooked the most basic document.ready function, which meant the script didn't even run.(事实证明,我忽略了最基本的document.ready函数,这意味着脚本甚至没有运行。)

In jQuery 1.xx it was written as:(在jQuery 1.xx中,其编写为:)
$(document).on('ready', function() {

})

I changed it to:(我将其更改为:)

$(document).ready(function() {

})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...