-5

我想问我是否可以将以下 JavaScript 代码合并到一个文件中?

当我输入一个 file.js

$(document).ready(function(){$('#slider-with-blocks-1').royalSlider({arrowsNav:true,arrowsNavAutoHide:false,fadeinLoadedSlide:false,controlNavigationSpacing:0,controlNavigation:'bullets',imageScaleMode:'none',imageAlignCenter:false,blockLoop:true,loop:true,numImagesToPreload:6,transitionType:'fade',keyboardNavEnabled:true,block:{delay:400} })  });

$(document).ready(function(){
$('.credit ').append(' credit by <span></span>');
$('.credit span')
.html($(&#39;<a/>&#39;).attr(&#39;href&#39;,&#39;www.google.com&#39;)
 .text(&#39;This is my link to google!&#39;)
 );
$(&#39;.credit span a&#39;)
.attr(&#39;id&#39;, &#39;credit&#39;);
});

$(&quot;[role=&#39;navigation&#39;]&quot;).flexNav(); 

window.onload = function() {
    if (!document.getElementById('credit') || document.getElementById('credit').href != "http://www.google.com/") {
        alert('Kreditnya jangan diilangin sob!');
    }
};
4

1 回答 1

2

将其存储在 js 文件中:

$(document).ready(function(){
    $('#slider-with-blocks-1').royalSlider(
        {arrowsNav:true,
         arrowsNavAutoHide:false,
         fadeinLoadedSlide:false,
         controlNavigationSpacing:0,
         controlNavigation:'bullets',
         imageScaleMode:'none',
         imageAlignCenter:false,
         blockLoop:true,
         loop:true,
         numImagesToPreload:6,
         transitionType:'fade',
         keyboardNavEnabled:true,
         block:{delay:400} });

    $('.credit ').append(' credit by &lt;span&gt;&lt;/span&gt;');
    $('.credit span').html('<a/>').attr('href:\\www.google.com').text('This is my link to google!');
    $('.credit span ').attr('id', 'credit');

    $("[role='navigation']").flexNav();

 });

$(window).load(function() {
    if (!document.getElementById('credit') || document.getElementById('credit').href != "http://www.google.com/") {
        alert('Kreditnya jangan diilangin sob!');
    }
)};

并调用它:

<script type="text/javascript" src="myFile.js"></script>

取消转义代码是一件好事,并正确缩进源代码:http ://en.wikipedia.org/wiki/Indent_style以提高可读性。

于 2012-12-27T01:37:38.827 回答