内联 JavaScript 触发器不起作用
inline javascript trigger doesn't work
html = '<a href="#" onclick="alert("This link is clickable only in the mobile!")">text</a>'
我附加了我的 html,但当我单击它时显示意外令牌 },是否不可能像那样执行内联触发器?
您需要
转义属性值中的"
字符...使用"
特殊实体:
html = '<a href="#" onclick="alert("This link is clickable only in the mobile!")">text</a>'
有更好的选择来实现这一点,而不是用JavaScript编写原始HTML。
j查询:
var aElement = jQuery("<a>").attr("href", "#").click(function() {
alert("This link is clickable only in the mobile!");
});
// You can append it like:
aElement.appendTo(document.body);
纯Javascript:
var aElement = document.createElement("a");
aElement.setAttribute("href", "#");
// If you want the onclick attribute to show up in HTML source:
aElement.setAttribute("onclick", "alert('"This link is clickable only in the mobile!'")");
// This way is easier:
aElement.onclick = function() {
alert("This link is clickable only in the mobile!");
}
// Append like:
document.body.appendChild(aElement);
不可能
完全像这样,因为您混合了双引号。
在字符串中使用转义的单引号:
html='<a href="#" onclick="alert(''This link is clickable only in the mobile!'')">text</a>'
html = '<a href="javascript:alert("This link is clickable only in the mobile!")">text</a>'
您可以在锚点的href
中直接附加javascript方法,还需要编码(转义(引号。
相关文章:
- 用键盘分页jQuery下一个和上一个控件.触发器('点击')不'不起作用
- 角度2触发器'单击'使用jquery不起作用
- 时间驱动触发器在 Google 电子表格中不起作用
- Qinit触发器函数在JQuery DatePicker上不起作用
- 为什么这个jQuery触发器不起作用
- 触发器JavaScript不起作用
- jQuery 键关闭触发器不起作用
- 滑动条触发器不起作用(也称为滑动条)
- JQuery 触发器单击事件不起作用
- 作为触发器的 Jquery 输入类型文件不起作用
- jQuery移动触发器“创建”不起作用,除非第一次
- UIB 工具提示自定义触发器不起作用
- typed.js jQuery 触发器在键入过程中不起作用
- 插入后触发器在从节点JS应用程序插入时不起作用
- 使用触发器“单击”下载附加到 的多个 CSV 文件不起作用
- j查询触发器按键不起作用
- jquery触发器在动态加载新内容后不起作用
- 简单事件触发器不起作用
- 内联 JavaScript 触发器不起作用
- 当子触发器触发时,同位素柱移不起作用