如何格式化JQuery的一个URL.Action()调用设置链接的href属性? [英] How to format a URL.Action() call in JQuery to set a link's href attribute?
本文介绍了如何格式化JQuery的一个URL.Action()调用设置链接的href属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要设置一个链接的href属性指向一个特定的形象,这已经从设置为标题的数据库的ID。不过,我有试图格式化字符串包括调用来获取图像的title属性的麻烦。
I need to set the href attribute of a link to point to a specific image, which has its id from the database set as its title. However, I am having trouble with trying to format the string to include a call to get the title attribute of the image.
下面是基本字符串:
$("#favoriteLink").hover(function() {
$(this).attr("href", '<%: Url.Action("FavoriteWallpaper", "Wallpaper", new{wallpaperId='+$(this).children.attr("title")+'}) %>');
});
favoriteLink是一个div和孩子只是一个图像。
favoriteLink is a div and the child is just one image.
推荐答案
哦,不,你不能因为你正在试图混合服务器端code和JavaScript。这个怎么样:
Oh no, you can't mix server side code and javascript as you are trying to. How about this:
$('#favoriteLink').hover(function() {
var title = $(this).children.attr('title');
$(this).attr('href', function() {
var url = '<%= Url.Action("FavoriteWallpaper", "Wallpaper", new { wallpaperId= "_TITLE_TO_REPLACE_"}) %>';
return this.href.replace('_TITLE_TO_REPLACE_', title);
});
});
这篇关于如何格式化JQuery的一个URL.Action()调用设置链接的href属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文