首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何连接脚本?

如何连接脚本?
EN

Stack Overflow用户
提问于 2020-03-17 20:14:25
回答 1查看 77关注 0票数 0

如果我正确理解,那么当进入页面https://localhost:44389/Page1/Index时,文本应该移动.

我跟踪链接,但什么也没发生。..。

我看到一个带有文本test的页面。

文本不移动。

代码 "ScriptTest.js","ScriptJS.js“。

代码语言:javascript
复制
var divxPos = 0;
window.onload = function () {
    this.runCode()
} 
function runCode() {
    var test = document.getElementById("");
    test.style.left = divxPos++ + 'px'; 
    setTimeout(() => runCode(), 50);
}

\Page1\Index.cshtml

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>    
    <meta charset="utf-8" />
    <title></title>    
    <script src ="~/ScriptJS.js"> </script>
    @*<script src ="~/JS/ScriptTest.js"> </script>*@

</head>
<body>
    <p id ="testElement"  style="position:absolute">test</p>

</body>
</html>

Startup.cs

代码语言:javascript
复制
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;


namespace WebAppCore
{
    public class Startup
    {

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

            app.UseDefaultFiles();
            app.UseStaticFiles();

        }
    }
}

码Page1Controller.cs

代码语言:javascript
复制
using Microsoft.AspNetCore.Mvc;

namespace WebAppCore.Controllers
{
    public class Page1Controller : Controller
    {
        public ActionResult Index()
        {
            return View();
        }


    }
}

项目结构

更新-1

Startup.cs.

  • I
  1. I更改了代码,创建了文件ScriptJS2.js。我正在启动一个debug.
  2. Result:,代码不工作。
  3. I更改了文件ScriptJS2.js的代码。我启动了debug.
  4. Result:
  5. ,代码不工作。我在文件step 2

  • 中看到了来自ScriptJS2.js的代码

问题.

  1. 如何使脚本工作?

  1. 如何使新代码出现在ScriptJS2.js文件中?

Startup.cs

代码语言:javascript
复制
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseDefaultFiles();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }

Code (步骤2)

代码语言:javascript
复制
var divxPos = 0;
window.onload = function () {
    var test = document.getElementById("testElement");
    test && runCode(test);
}
function runCode(element) {
    element.style.left = divxPos++ + 'px';
    setTimeout(() => runCode(element), 50);
}

Code (步骤5)

代码语言:javascript
复制
var divxPos = 0;
window.onload = function () {
    this.runCode()
}
function runCode() {
    var test = document.getElementById("testElement");
    test.style.left = divxPos++ + 'px';
    setTimeout(() => runCode(), 50);
}

图片-1

图片-2

图片-3

更新-2

控制台

1.用于从加载资源的连接使用TLS1.0或TLS1.1,它们被废弃,并将在将来禁用。一旦禁用,用户将无法加载这些资源。服务器应该启用TLS 1.2或更高版本。有关详细信息,请参阅。2:44389/最惠国:1

2.未能加载资源:服务器响应状态为404 (未找到)

chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.preload.js.map 3. DevTools未能解析SourceMap: DevTools

chrome-extension://gighmmpiobklfepjocnamgkkbiglidom/include.postload.js.map 4. DevTools未能解析SourceMap: DevTools

图片-1

更新3

我创建了一个ASP.NET核心Visual模板应用程序。

密码有效了。

文本test在页面周围移动。

但!

我想得到一个没有多余的应用程序。

我想申请的只有:

控制器,Index.cshtml,ScriptJS2.js

问题.

我可以从Update-3中创建的应用程序中删除哪些额外的文件、文件夹?

图片-1

图片-2

EN

回答 1

Stack Overflow用户

发布于 2020-03-18 05:42:16

您需要使用var test = document.getElementById("testElement")代替。

此外,最重要的是需要将静态文件中间件放在端点路由中间件之前,将startup.cs更改为

代码语言:javascript
复制
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseDefaultFiles();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60729588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档