如何从我的HTML标记中分离jQuery代码

时间:2011-07-07 23:04:09

标签: php jquery

使用php开发应用程序,我为页眉和页脚创建了一个简单的模板系统。 我的困境是我有很多视图依赖于jQuery代码,他们都使用标题模板。这里是标题的摘录。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>NgInformr | Adminstration | <?php if(isset($title)) { echo $title; }?></title>
<?=$html->js("javascripts/top_up")?>
<?=$html->css("admin-css")?>
<?=$html->css("form-style")?>
<link href="http://localhost/nginformr/www_root/js/uploadify/uploadify.css" type="text/css" rel="stylesheet"/>
<?=$html->css("base/jquery.ui.all")?>
<?=$html->js("uploadify/jquery.uploadify.v2.1.4.min")?>
<?=$html->js("uploadify/swfobject")?>
<?=$html->js("jquery-ui-1.8.9")?>
<?=$html->js("jscripts/tiny_mce/jquery.tinymce")?>
<script type="text/javascript">
$(document).ready(function() {
//Uploader
$('#file_upload').uploadify({
    uploader : "/nginformr/www_root/js/uploadify/uploadify.swf",
    script  : "/nginformr/www_root/js/uploadify/uploadify.php",
    folder : "/nginformr/www_root/uploads",
    auto : true,
    onComplete : function(event, queueID, fileObj, reponse, data) {
        $('#file_details').css({display:"block"});
        $('#path').val(fileObj.filePath);
        $('#type').val(fileObj.type);
    },
    onError    : function (event,ID,fileObj,errorObj) {
      alert(errorObj.type + ' Error: ' + errorObj.info);
    }
});
$("#side_menu").accordion({header:"p",fillSpace: false});
//Ajax for select boxes
$('#state').change(function() {
        $('#city').html("");
        var id = $('#state').val();
        var dataString = "id="+id;
    $.ajax({
        url:"http://localhost/nginformr/cities/ajax_cities",
        type:"POST",
        data:dataString,
        success:function(html) {
            $('#city').append(html);
        }
    });
});
$("#city").change(function() {
    $('#area').html("");
    var id = $('#city').val();
    var dataString = "id="+id;
    $.ajax({
        url:"http://localhost/nginformr/areas/ajax_areas",
        type:"post",
        data:dataString,
        success:function(html) {
            $('#area').append(html);
        }
    });
});
$('#sector').change(function() {
    $('#category').html("");
    var id = $('#sector').val();
    var dataString = "id="+id;
    $.ajax({
        url:"http://localhost/nginformr/categories/ajax_categories",
        type:"post",
        data:dataString,
        success:function(html) {
            $('#category').append(html);
        }
    });
});
$(".widget").draggable();

//TinyMCE Editor
$('textarea.tinymce').tinymce({
    // Location of TinyMCE script
    script_url : 'http://localhost/nginformr/www_root/js/jscripts/tiny_mce/tiny_mce.js',

    // General options
    theme : "advanced",
    plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

    // Theme options
    theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
    theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
    theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,

    // Example content CSS (should be your site CSS)
    //content_css : "css/content.css",

    // Drop lists for link/image/media/template dialogs
    template_external_list_url : "lists/template_list.js",
    external_link_list_url : "lists/link_list.js",
    external_image_list_url : "lists/image_list.js",
    media_external_list_url : "lists/media_list.js",

    // Replace values for the template plugin
    template_replace_values : {
        username : "Some User",
        staffid : "991234"
    }
});
});
</script>
<script type="text/javascript">
  TopUp.host = "http://localhost/nginformr/";
  TopUp.images_path = "www_root/img/top_up/";
</script>
</head>
<body>
<div id="header"><!-- ------Header -->
<div id="logo">
<a href="./"><?$html->img("nginformr_logo.png") ?></a>
</div>

<div id="user_controls">
<?=$html->link("admin/users/logout","Logout")?>
</div>
</div>

3 个答案:

答案 0 :(得分:1)

如果您的意思是将jQuery代码从标题中移出并放入单独的文件中,您只需将其全部复制并粘贴(减去HTML脚本标记)到网站文件夹中的javascript文档中(例如general.js)

然后,以与引用其他javascript文件相同的方式从HTML引用该文件。

<?=$html->js("general.js")?>

答案 1 :(得分:0)

为什么不创建一个Javascript文件并将其包含在HTML中?

因此,将所有JQuery代码复制粘贴到(例如)jquery_functions.js并包含它:

<html>
    <head>
        ...
        <script type="text/javascript" language="javascript" src="/javascript/jquery_functions.js"></script>
        ...
    </head>
    <body>
        ...
    </body>
</html>

或者使用您在代码示例中使用的功能

<?=$html->js("/javascript/jquery_functions")?>

答案 2 :(得分:0)

我不确定为什么你不把你的jQuery代码放到它自己的js文件中并让你的模板包含它。