我有个功能可以滚动到锚上:
function scrollToAnchor(aid) {
var aTag = $("a[name='" + aid + "']");
if (aTag.length) {
$('html,body').animate({
scrollTop: aTag.offset().top - 100
}, 'slow');
aTag.closest('.subpanel').effect("highlight", 5000);
}
}HTML
<a id="A2" class="gridLabel" name="Add Action Item">
<span id="MainContent_Label19" title="Add / Edit an action item.">Add / Edit Action Item</span>
</a>我必须调用执行某种操作的服务器端事件。一旦操作完成,我需要调用这个scrollToAnchor。我试过这个:
ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", "$(function(){
function scrollToAnchor(aid) { var aTag = $('a[name=''' + aid + ''']');if (aTag.length)
{$('html,body').animate({ scrollTop: aTag.offset().top - 100 },
'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show();
scrollToAnchor('Add Action Item');});", true);但是,我在console中会出现错误,可能是因为我的' '和" "。有人能帮我整理一下这个吗。
我也试过:
ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", "$(function()
{function scrollToAnchor(aid) { var aTag = $('a[name=\"' + aid + '\"]' + ']');if
(aTag.length) {$('html,body').animate({ scrollTop: aTag.offset().top - 100 },
'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show();
scrollToAnchor('Add Action Item');});", true);发布于 2014-02-11 14:11:51
问题似乎就在这里:
'a[name=''' + aid + ''']'试着将其替换为:
'a[name=' + aid + ']'或者如果您需要名称值中的引号:
'a[name=\'' + aid + '\']'
// Or
'a[name=\"' + aid + '\"]'发布于 2014-02-11 14:13:52
我要做的是将函数放在页面(或页面加载的js文件中),然后简单地注册一个脚本,如下所示:
ScriptManager.RegisterClientScriptBlock(this, GetType(),
"OpenActions", "scrollToAnchor('Add Action Item');", true);据我所知,您不需要在每个服务器端事件上注册整个脚本。您只需要使用给定的参数来运行该函数。
发布于 2014-02-11 14:13:08
以@作为字符串的前缀,如下所示
ScriptManager.RegisterClientScriptBlock(this, GetType(), "OpenActions", @"$(function(){
function scrollToAnchor(aid) { var aTag = $('a[name=' + aid + ']');if (aTag.length)
{$('html,body').animate({ scrollTop: aTag.offset().top - 100 },
'slow');aTag.closest('.subpanel').effect('highlight', 5000);}} $('#tblAction').show();
scrollToAnchor('Add Action Item');});", true);https://stackoverflow.com/questions/21704179
复制相似问题