【问题标题】:How to access html function from Script# code?如何从 Script# 代码访问 html 函数?
【发布时间】:2015-08-04 00:48:07
【问题描述】:

在 Script# 中如何访问页面上 html 中定义的函数?

在 html 页面上是这个预先存在的功能:

<script type='text/javascript'>
    function makeStuffHappen(selector)
    {
           alert('stuff happening');
    }
</script>
<div id='#usefulplace'></div>

我从 scriptsharp 加载生成的脚本,并且需要访问该生成脚本中的“全局”makeStuffHappen 函数。基本上,从 Scriptsharp 我需要能够调用该函数 makeStuffHappen("#usefulplace");

如何从 scriptsharp 中调用 makeStuffHappen?

【问题讨论】:

  • 请添加更多上下文,例如scriptsharp 和那个函数如何重合?它是同一类的定义方法吗?如果不是,该页面来自哪里,当前使用什么代码与该页面交互?另外,我自己对 script# 不太熟悉,但这可能是重复的:stackoverflow.com/questions/13149273/…。也许相关:stackoverflow.com/questions/13135498/…
  • 在发布问题之前,我实际上已经阅读了这两个内容,我会尽可能地添加上下文,但要点是我正在从 scriptsharp 加载 javascript 到页面上并需要它来调用已经在页面上定义纯(不是scriptsharp)函数
  • 除了投反对票之外,投反对票的人可以发表评论,以便我知道如何改进我的问题吗?

标签: javascript html script#


【解决方案1】:

我想出了怎么做。我创建了一个 scriptsharp 类文件,并做了 IgnoreNamespace、Imported 和 ScriptAlias 这三个,让它映射到 html 中的函数,如下:

using System.Runtime.CompilerServices;

namespace TestNamespace
{
    /// <summary>
    /// 
    /// </summary>
    [IgnoreNamespace]
    [Imported]
    public static class ImportedPageFunctions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selector"></param>
        [ScriptAlias("makeStuffHappen")]
        public static void makeStuffHappen(string selector)
        {

        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多