如何使用内部 HTML 传递 JavaScript 参数 [英] How to pass JavaScript arguments using inner HTML

查看:20
本文介绍了如何使用内部 HTML 传递 JavaScript 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaScript 新手,目前遇到了一个问题.基本上,我使用 API 来搜索动漫,获取 html 图像、概要和名称并将它们放在表格中.这很好用.我想要做的是为表中的每个名称添加一个 onlick 以执行使用动漫名称作为参数的功能.我曾尝试使用转义字符\'",但没有奏效.这是我的每一行代码:

I am new to JavaScript and I am currently stuck on an issue. Basically, I using an API to search for anime, get the html image, synopsis, and name and put them in a table. That works fine. What I am trying to do is add an onlick for each name in the table to perform a function using the name of the anime as an argument. I have tried using "\'", the escape character, and it didn't work. Here is my code for each row:

nameAndSynopsis.innerHTML = "<table style='border: 1px solid black'><tr><td style='color:blue' onclick='getEpisodes(\'" + anime_name + "\')'>" + anime_name + "</td></tr><br><br><br><tr><td>" + anime_synopsis + "</td></tr></table>";`

当我运行代码时,控制台显示 Unexpected end of input,这是我在网页上使用检查时源选项卡的结果

When I run my code, the console says Unexpected end of input and here are the results from the source tab when I use inspect on my web page

`getEpisodes(`

任何帮助将不胜感激.

推荐答案

尝试使用 模板文字 而不是像这样的所有引号.

Try using template literals instead of all the quotes like this.

nameAndSynopsis.innerHTML = `<table style='border: 1px solid black'><tr><td style='color:blue' onclick=getEpisodes(${anime_name})> ${anime_name} </td></tr><br><br><br><tr><td>${anime_synopsis}</td></tr></table>`

这篇关于如何使用内部 HTML 传递 JavaScript 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆