问题描述
所以我试图在我的页面中创建一个链接,但由于某种原因它的代码是脱节的
so I am trying to create a link within my page but for some reason it is getting disjointed the code is
data = data + "<li><a href='#' onclick='History.pushState({state:null},'article,"+rtndata[j].id+"','article'); return false;'>" + rtndata[j].title + "</a></li>";
rtndata [j] .id是一个id号,rtndata [j] .title是一个标题从数据库中提取但是当它在浏览器中呈现时它变为
rtndata[j].id is an id number and rtndata[j].title is a title both are being pulled from a db but when it renders in the browser it becomes
<a false;'="" return="" article,41','article');="" onclick="History.pushState({state:null}," href="#">Newsflash 5</a>
所以我要呈现的是
<a href="#" onclick="History.pushState({state:null},'article,41','article'); return false;'>"Newsflash 5</a>;
任何人都可以帮助我吗?
Can anyone help me?
推荐答案
onclick
属性的值用单引号括起来。因此,你必须使用如果您必须在属性中使用引号,则code>或
& quot;
(仅限值)。
The value of the onclick
attribute is enclosed within single quotes. Because of this, you have to either use "
or "
(only for values) if you have to use quotes inside the attribute.
因为您的JavaScript字符串包含在双引号()中,双引号必须先转义才能使用:
.... onclick ='... \....'*;
。
Because your JavaScript string is enclosed within double quotes ("
), the double quotes have to be escaped before they can be used: "....onclick='...\"....'....";
.
为了修复你的代码,我决定使用双引号(转义)而不是单引号:
To fix your code, I have decided to use double quotes (escaped) instead of single quotes:
data = data + "<li><a href='#' onclick=\"History.pushState({state:null},'article,"+rtndata[j].id+"','article'); return false;\">" + rtndata[j].title + "</a></li>";
详细信息表:
JS string marker Attribute marker Valid attribute value markers
" \" ' "
" ' \" "
' " \' "
' \' " "
这篇关于在其中创建一个包含javascript的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!