在所有页面的母版页中都无法识别JQuery

时间:2015-02-13 20:37:41

标签: jquery html asp.net master-pages

检测到问题:

网络选项卡中的请求URL正在向页面中的路径添加另一个目录,该目录无法正常工作,因为webform嵌套在解决方案结构中的其他目录中。

主页不会将此附加目录添加到请求URL,因为webform不嵌套在解决方案结构中的其他目录中。

如何将其作为母版页中脚本的绝对路径?

原始问题:

我遇到某些页面无法识别jQuery的问题。我试图弄清楚为什么错误" ReferenceError:$未定义"在我的控制台中显示,在某些页面中引用页面js文件引用,而不是其他页面。

我的母版页如下:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="UseSage.SiteMaster" %>

<!DOCTYPE html>
<html class=" js flexbox canvas canvastext webgl no-touch geolocation postmessage websqldatabase indexeddb hashchange history draganddrop websockets rgba hsla multiplebgs backgroundsize borderimage borderradius boxshadow textshadow opacity cssanimations csscolumns cssgradients cssreflections csstransforms csstransforms3d csstransitions fontface generatedcontent video audio localstorage sessionstorage webworkers applicationcache svg inlinesvg smil svgclippaths" lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <title><%: Page.Title %></title>
    <link href="~/Content/Site.css" rel="stylesheet" />
    <link href="~/Content/wp-content/themes/sage/favicon.png" rel="shortcut icon" type="image/png" />    

    <script src="Scripts/jquery-2.1.3.js"></script>

    <script src="Scripts/jquery-ui-1.8.24.js"></script>
    <script src="Scripts/modernizr-2.6.2.js"></script>

    <link href="SageWP/Scripts/wp-content/plugins/styles-with-shortcodes/css/style.css" rel="stylesheet" /> 
    <link href="SageWP/Scripts/wp-content/plugins/styles-with-shortcodes/editorplugin/rh_columns.css" rel="stylesheet" />
    <link href="SageWP/Scripts/wp-content/plugins/styles-with-shortcodes/editorplugin/rh_columns_ver=1.0.2.css" rel="stylesheet" />
    <link href="SageWP/Scripts/wp-content/plugins/styles-with-shortcodes/js/lightbox_themes_default/jquery.lightbox.css" rel="stylesheet" />
    <link href="SageWP/Scripts/wp-content/plugins/Wordpress-seo%20css/adminbar.css" rel="stylesheet" />    
    <link href="SageWP/Scripts/wp-includes/css/admin-bar.min.css" rel="stylesheet" />
    <link href="Content/wp-content/themes/roots/assets/css/app.css" rel="stylesheet" />
    <link href="SageWP/Scripts/wp-content/themes/sage/style.css" rel="stylesheet" />

    <script src="SageWP/Scripts/wp-content/plugins/styles-with-shortcodes/js/sws_frontend.js"></script>
    <script src="SageWP/Scripts/wp-content/themes/roots_assets_js/vendor/modernizr-2.6.2.min.js"></script>
    <script src="SageWP/Scripts/wp-content/themes/roots_assets_js/plugins.js"></script>
    <script src="SageWP/Scripts/wp-content/themes/sage/assets_js/main.js"></script>
    <script src="SageWP/Scripts/wp-includes/js/jquery/admin-bar.min.js"></script>


    <asp:PlaceHolder runat="server">
        <script src="<%: ResolveUrl("~/Scripts/modernizr-2.6.2.js") %>"></script>
    </asp:PlaceHolder>
    <meta name="viewport" content="width=device-width" />
    <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>
    <%--<form runat="server">--%>
        <div>
            <header class="banner" role="banner">
                <div class="container">
                    <a class="brand" href="http://www.usesage.com/">
                        <img src="http://www.usesage.com/wp-content/themes/sage/assets/img/sagex2.png" alt="Sage Technologies" title="Sage Technologies" width="111px" height="40px" />
                    </a>
                    <nav class="nav-main" role="navigation">
                        <ul id="menu-primary-navigation" class="nav nav-pills">
                            <li class="dropdown menu-about-acos"><a class="dropdown-toggle" href="/About_ACOs/AboutACOs.aspx">About ACOs</a>
                                <ul class="dropdown-menu">
                                    <li class="desktop-hide menu-about-acos"><a href="/About_ACOs/AboutACOs.aspx">About ACOs</a></li>
                                    <li class="desktop-hide menu-overview"><a href="/About_ACOs/AboutACOs.aspx">Overview</a></li>
                                    <li class="menu-aco-building-blocks"><a href="/About_ACOs/ACOBuildingBlocks.aspx">ACO Building Blocks</a></li>
                                    <li class="menu-ideal-state"><a href="/About_ACOs/IdealState.aspx">Ideal State</a></li>
                                </ul>
                            </li>
                            <li class="dropdown menu-about-us"><a class="dropdown-toggle" href="/About.aspx">About Us</a>
                                <ul class="dropdown-menu">
                                    <li class="desktop-hide menu-about-us"><a href="/About.aspx">About Us</a></li>
                                    <li class="desktop-hide menu-overview"><a href="/About.aspx">Overview</a></li>
                                    <li class="menu-our-leadership"><a href="/about-us/our-leadership/">Our Leadership</a></li>
                                    <li class="menu-our-clientstestimonials"><a href="/about-us/our-clientstestimonials/">Our Clients/Testimonials</a></li>
                                    <li class="menu-case-studies"><a href="/about-us/case-studies/">Case Studies</a></li>
                                </ul>
                            </li>
                            <li class="dropdown menu-solutions"><a class="dropdown-toggle" href="/Solutions.aspx">Solutions</a>
                                <ul class="dropdown-menu">
                                    <li class="desktop-hide menu-solutions"><a href="/Solutions.aspx">Solutions</a></li>
                                    <li class="desktop-hide menu-overview"><a href="/Solutions.aspx">Overview</a></li>
                                    <li class="menu-clinical-integration"><a href="/solutions/clinical-integration/">Clinical Integration</a></li>
                                    <li class="menu-case-disease-management"><a href="/solutions/case-disease-management/">Case &#038; Disease Management</a></li>
                                    <li class="menu-population-management"><a href="/solutions/population-management/">Population Management</a></li>
                                    <li class="menu-business-intel-analytics"><a href="/solutions/business-intel-analytics/">Business Intel &#038; Analytics</a></li>
                                    <li class="menu-risk-management"><a href="/solutions/risk-management/">Risk Management</a></li>
                                    <li class="menu-health-services-consultation"><a href="/solutions/health-services-consultation/">Health Services Consultation</a></li>
                                </ul>
                            </li>
                            <li class="dropdown menu-aco-implementation"><a class="dropdown-toggle" href="/ACO_Implementation.aspx">ACO Implementation</a>
                                <ul class="dropdown-menu">
                                    <li class="desktop-hide menu-aco-implementation"><a href="/ACO_Implementation.aspx">ACO Implementation</a></li>
                                    <li class="desktop-hide menu-overview"><a href="/ACO_Implementation.aspx">Overview</a></li>
                                    <li class="menu-90-days-to-an-aco"><a href="/aco-implementation/90-days-to-an-aco/">90 Days to an ACO</a></li>
                                    <li class="menu-customizable-scalable"><a href="/aco-implementation/customizable-scalable/">Customizable &#038; Scalable</a></li>
                                    <li class="menu-technical-architecture"><a href="/aco-implementation/technical-architecture/">Technical Architecture</a></li>
                                    <li class="menu-symphony-partnership"><a href="/aco-implementation/symphony-partnership/">Symphony Partnership</a></li>
                                    <li class="menu-custom-applications"><a href="/aco-implementation/custom-applications/">Custom Applications</a></li>
                                </ul>
                            </li>
                            <li class="desktop-hide menu-contact-us"><a href="/Contact.aspx">Contact Us</a></li>
                            <li class="desktop-hide menu-faqs"><a href="/Support.aspx">FAQs</a></li>
                            <li class="desktop-hide menu-news"><a href="/News.aspx">News</a></li>
                            <li class="desktop-hide menu-login"><a href="/Login.aspx">Login</a></li>
                            <li class="desktop-hide active menu-home"><a href="/">Home</a></li>
                        </ul>
                    </nav>
                    <div class="utility">
                        <ul id="menu-utility-menu" class="utility-menu" style="display: inline-block">
                            <li class="active menu-home"><a href="/Home.aspx">Home</a></li>
                            <li class="menu-contact-us"><a href="/Contact.aspx">Contact Us</a></li>
                            <li class="menu-faqs"><a href="/Support.aspx">FAQs</a></li>
                            <li class="menu-news"><a href="/News.aspx">News</a></li>
                            <li class="menu-login"><a href="/Login.aspx">Login</a></li>
                            <li><a class="doc-link" href="https://www.usesage.com/SageWebsite/FindProvider.aspx" target="_blank">Find a Doctor</a></li>
                            <li>
                                <fieldset>
                                    <form role="search" method="get" id="searchform" action="http://www.usesage.com">
                                        <input type="text" value="" name="s" id="s" runat="server" style="height: 19px; width: 150px"/>
                                        <span class="search-submit-container">
                                            <input type="submit" id="searchsubmit" value="" class="btn" style="width:100px;margin:0 auto;display:block;">
                                        </span>
                                    </form>
                                </fieldset>
                            </li>
                        </ul>
                    </div>
                </div>
                <a class="mobile-menu-link" href="javascript:void(0)">&nbsp;</a>
            </header>
        </div>
        <div id="body">
            <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        </div>            
</body>
</html>

在一个实例中,jQuery不能在此页面中工作。

HTML如下:

<%@ Page Title="Ideal State - Sage" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="IdealState.aspx.cs" Inherits="UseSage.About_ACOs.IdealState" %>

<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">

    <%--<script src="/PageHelpers/ideal_state.js"></script>--%>
    <script type="text/javascript">
        $(function () {
            $('body').addClass('page page-id-125 page-child parent-pageid-124 ideal-state');
        });
    </script>
    <div class="wrap container" role="document">
        <div class="content row">
            <div class="main span12" role="main">
                <div class="page-header">
                    ...
                </div>
                <section class="body-content">
                    ...
                </section>
                <div class="page-footer-banner"></div>
                <div class="subnav-container">
                    <div class="subnav-inner">
                        <ul>
                            <li class="page_item page-item-127"><a href="/AboutACOs/ACOBulidingBlocks.aspx">ACO Building Blocks</a></li>
                            <li class="page_item page-item-636"><a href="/about-acos/download-whitepaper/">Download White Paper</a>
                                <ul class='children'>
                                    <li class="page_item page-item-638"><a href="/about-acos/download-whitepaper/thank-you/">Thank You</a></li>
                                </ul>
                            </li>
                            <li class="page_item page-item-631"><a href="/About-ACOs/Contact.aspx">Contact Us</a>
                                <ul class='children'>
                                    <li class="page_item page-item-633"><a href="/about-acos/contact-us/thank-you/">Thank You</a></li>
                                </ul>
                            </li>
                            <li class="page_item page-item-125 current_page_item"><a href="/About_ACOs/IdealState.aspx">Ideal State</a></li>
                        </ul>
                    </div>
                </div>
            </div>
            <!-- /.main -->
        </div>
        <!-- /.content -->
    </div>
    <!-- /.wrap -->
</asp:Content>

在一个实例中,jQuery在此页面中正常工作。

HTML如下:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="UseSage.Home" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <script src="PageHelpers/home.js"></script>

    <a href="Content/Home/">Content/Home/</a>

        <div class="wrap container" role="document">
            <div class="content row">
                <div class="main span12" role="main">
                    <h1>Making ACO implementation a dream, not a nightmare.</h1>
                    <div class="page-title-banner">&nbsp;</div>
                    <!-- <div class="page-title-banner"></div> -->
                    <div class="home-element-container">

                        <!-- Start the Loop. -->

                        <article class="home-block">
                            <div class="text-content">
                                <h2>Bewilderment is exactly what you don&#8217;t need.</h2>
                                <p>The ceaselessly changing world of health care regulation is confusing enough without our adding to the chaos. Instead, we’re an oasis of order bringing understanding to the process. Payers are asking providers of healthcare to perform administrative &#8211; insurance type services and to be at risk for meeting quality and cost requirements.</p>
                                <button onclick="location.href='/aco-implementation/'">Learn about how we implement ACOs</button>
                            </div>
                            <img width="370" height="370" src="/Content/Home/art-1.png" class="attachment-post-thumbnail wp-post-image" alt="Learn about how we implement ACOs">
                        </article>

                        <article class="home-block">
                            <div class="text-content">
                                <h2>We don&#8217;t just talk about getting things done, we actually get them done.</h2>
                                <p>Some people talk, others do. We fall squarely in the latter camp. Rather than merely spouting the latest buzzwords and catchphrases, we come to work prepared to actually get to work on implementing solutions for all aspects of your business. Payers currently use Sage to help providers meet such requirements as cost efficiency, quality, patient information and regulatory changes.</p>
                                <button onclick="location.href='/solutions/'">Learn about our solutions</button>
                            </div>
                            <img width="401" height="640" src="/Content/Home/art-2.png" class="attachment-post-thumbnail wp-post-image" alt="Learn about our Soultions">
                        </article>

                        <article class="home-block">
                            <div class="text-content">
                                <h2>The clock is ticking. We help you build an ACO that fits&#8230;fast.</h2>
                                <p>&#8220;One size fits all&#8221; is great in theory, but a disaster the first time you try on a shoe that&#8217;s two sizes too small. The same is true of companies that try to force-fit a solution for your company with no regard for the customization that&#8217;s required to really make it right for you. Do you really want a “one size fits all&#8221; ACO?</p>
                                <p>We step into the shoes of what the payer or providers need to get done and implement the Sage toolbox to meet operational and regulatory requirements.</p>
                                <button onclick="location.href='/about-acos/'">Learn about ACOs</button>
                            </div>
                            <img width="235" height="371" src="/Content/Home/art-3.png" class="attachment-post-thumbnail wp-post-image" alt="Learn about ACOs">
                        </article>

                    </div>
                </div>
                <!-- /.main -->
            </div>
            <!-- /.content -->
        </div>
        <!-- /.wrap -->

        <script type='text/javascript' src='SageWP/Scripts/wp-content/themes/roots_assets_js/plugins.js'></script>
        <script type='text/javascript' src='SageWP/Scripts/wp-content/themes/sage/assets_js/main.js'></script>
</asp:Content>

3 个答案:

答案 0 :(得分:0)

您需要使脚本路径相对,以便可以从任何内容页面访问它们,无论其路径如何。

<script src="~/Scripts/jquery-2.1.3.js"></script>

答案 1 :(得分:0)

您在母版页中使用的文件的路径需要是文件的路径,因为它将在任何页面中使用母版页。

您需要对脚本文件使用绝对URL,而不是现在使用的相对路径。您可以通过多种方式实现此目的,但最简单的方法可能就是将所有客户端<link><script>元素转换为服务器端生成的元素。你可以抓住System.Web.Optimization NuGet并开始使用捆绑包,如果你想要更高级的话。

"~/"为您提供绝对的起始路径,该路径还会考虑您应用的根位置(虚拟根目录),但只能在将要处理的网址中使用通过ASP.Net。因此,在页面或代码隐藏中生成HtmlLink,转换这些 -

<link href="~/Content/Site.css" rel="stylesheet" />

进入这种形式 -

<asp:HtmlLink ID="whatever" runat="server" Href="~/Content/Site.css" rel="stylesheet" type="text/css" />

或此表格 -

HtmlLink sheet = new HtmlLink();
sheet.Href = "~/Content/Site.css";
sheet.Attributes.Add("type", "text/css");
sheet.Attributes.Add("rel", "stylesheet");

Page.Header.Controls.Add(sheet);

我不认为<script>元素具有特定的控件,因为他们希望您使用脚本管理器类,但您可以使用代码隐藏中的通用控件。

<script src="Scripts/jquery-2.1.3.js"></script>

HtmlGenericControl jscript = new HtmlGenericControl("script");
jscript.Attributes.Add("src", Server.MapPath("~/Scripts/jquery-2.1.3.js"));
jscript.Attributes.Add("type", "text/javascript");

Page.Header.Controls.Add(jscript);

答案 2 :(得分:0)

我遇到了同样的问题。对于主页js文件正在工作,但如果应用程序移动到某个内页,它无法正常工作。我找到了解决方案,请找到以下代码。如果它适合你,请投票给我。

import UIKit
import CoreData

class ToDoTableViewController: UITableViewController {
var listItems = [NSManagedObject]()

override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.add, target: self , action: #selector(ToDoTableViewController.addItem))
}

func addItem(){
let alertController = UIAlertController(title: "To Do Tasks Lists!!!!", message: "Write Down...", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Confirm", style: UIAlertActionStyle.default, handler: ({
    (_) in
    if let field = alertController.textFields![0] as? UITextField {
        self.saveItem(itemToSave: (field.text!))
        self.tableView.reloadData()
    }
    }
))

let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil)
alertController.addTextField(configurationHandler: ({
    (textField) in
    textField.placeholder = "Type in Something!!!!"
}))

alertController.addAction(confirmAction)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
}

func saveItem(itemToSave : String){
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let entity = NSEntityDescription.entity(forEntityName: "ListEntity", in: managedContext)
let item = NSManagedObject(entity: entity!, insertInto: managedContext)
item.setValue(itemToSave, forKey: "item")
do {
    try managedContext.save()
    listItems.append(item)
}
catch {

    print("Error")
}
}

override func viewWillAppear(_ animated: Bool) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.managedObjectContext
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "ListEntity")

do{
    let results = try managedContext.fetch(fetchRequest)
    listItems = results as! [NSManagedObject]
}
catch {
    print("Error")
}
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let managedContext = appDelegate.managedObjectContext
    let objectToDelete = listItems[indexPath.row]
    listItems.remove(at: indexPath.row)
    managedContext.delete(objectToDelete)
    tableView.deleteRows(at: [indexPath], with: .fade)
    do {
        try managedContext.save()
    }
    catch {
        print("Error")
    }
}
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")! as UITableViewCell
let item = listItems[indexPath.row]
cell.textLabel?.text = item.value(forKey: "item") as! String?
cell.backgroundColor = UIColor.clear
return cell
}

func getIndexPathForSelectedCell() -> IndexPath?
{
var indexPath2:IndexPath?
if tableView.indexPathsForSelectedRows!.count > 0 {
    indexPath2 = tableView.indexPathsForSelectedRows![0]
}
return indexPath2
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "detailView")
{
    if let indexPath2 = getIndexPathForSelectedCell()
    {
        // here write code for move to next controller.
        let vc = segue.destination as! TextEditViewController
        //vc.FirstString =  listItems[(indexPath2 as NSIndexPath).row] as String

        let item = listItems[(indexPath2 as NSIndexPath).row]
        vc.FirstString = (item.value(forKey: "item") as! String?)!
    }
}
}

}


//TextEditViewController
import UIKit
import CoreData
class TextEditViewController: UIViewController {
@IBOutlet weak var textEdit: UITextView!
var FirstString = String()    

override func viewDidLoad() {
super.viewDidLoad()
textEdit.text = FirstString
print(self.FirstString)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}