使用jquery时,mvc局部视图会丢失对images文件夹的跟踪



这就是我所拥有的,它可以工作:

$(function(){
$('.slide-out-div').tabSlideOut({
        tabHandle: '.handle',                     //class of the element that will become your tab
        pathToTabImage: 'http://mhmiisdev2/images/contact_tab.gif', //path to the image for the tab //Optionally can be set using css
        imageHeight: '122px',                     //height of tab image           //Optionally can be set using css
        imageWidth: '40px',                       //width of tab image            //Optionally can be set using css
        tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
        speed: 300,                               //speed of animation
        action: 'click',                          //options: 'click' or 'hover', action to trigger animation
        topPos: '200px',                          //position from the top/ use if tabLocation is left or right
        leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
        fixedPosition: true                      //options: true makes it stick(fixed position) on scroll
    });
});

这就是我想要的,当我从一个控制器换到另一个控制器时,它不起作用。注意,图像路径不是绝对

$(function(){
    $('.slide-out-div').tabSlideOut({
        tabHandle: '.handle',                     //class of the element that will become your tab
        pathToTabImage: '/images/contact_tab.gif', //path to the image for the tab //Optionally can be set using css
        imageHeight: '122px',                     //height of tab image           //Optionally can be set using css
        imageWidth: '40px',                       //width of tab image            //Optionally can be set using css
        tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
        speed: 300,                               //speed of animation
        action: 'click',                          //options: 'click' or 'hover', action to trigger animation
        topPos: '200px',                          //position from the top/ use if tabLocation is left or right
        leftPos: '20px',                          //position from left/ use if tabLocation is bottom or top
        fixedPosition: true                      //options: true makes it stick(fixed position) on scroll
    });
});

完整性的html。。。。

<div class="slide-out-div">
    <a class="handle" href="http://link-for-non-js-users.html">Content</a>
    <h3>Medical Variance Reports</h3>
    <div>
        <ul>
            <li><a href="http://mhmssrs2/Reports/Pages/Report.aspx?" target="_blank">Individual Medicines</a></li>
        </ul>
    </div>
</div>

我怀疑pathToTabImage: /images/contact_tab.gif'在浏览控制器时不知何故丢失了上下文。帮我理解。。。

我怀疑pathToTabImage:/images/contact_tab.gif不知怎么丢失了它浏览t个控制器时的上下文。

哦,是的,你是对的。在ASP.NET MVC中处理url时始终使用url助手:

pathToTabImage: '@Url.Content("~/images/contact_tab.gif")'

永远不要像在代码中那样硬编码url。这将确保您的应用程序在虚拟目录下的IIS中部署时能够正常工作。

只需在JS中使用助手Url.Content()函数,如

pathToTabImage: @Url.Content("~/images/contact_tab.gif")

如果你的JS在一个外部Javascript文件中,你只需要引用一个全局变量。

在视图顶部:

var myPath=@Url.Content("~/images/");

外部JS文件内部:pathToTabImage: myPath + "contact_tab.gif"

以下是我所做的

 alert(UrlContent("/hdd/images.gif")); //for debugging purposes 
 alert(UrlContent("~/hdd/images.gif"));
 alert(UrlContent("hdd/images.jpg"); 
 //they all return the correct url 
function UrlContent(url) {

// first lets take care of users that are smart
url = $.trim(url.replace("~/", "")); //this should take care of smart users!
if (url.charAt(0) == "/") {
    url=url.substring(1,url.length)
}
//now that we have taken care of smart users lets form the url
var UrlPath = location.protocol + '//' + location.hostname +location.port+'/'+url
return UrlPath

}

最新更新