我应该如何用Java语言编写一条语句来在页面中生成JavaScript
out.write("document.write('< a href='str'> '+str.slice(beg+1,end)+' </a>');");以便在JavaScript中创建语句
document.write("< a href=' "+str+" '> "+str.slice(beg+1,end)+" </a>"); //< a链接将转到其地址存储在字符串中的页面
目前,它将href值作为字符串,而不是字符串中存储的值,即它正在搜索页面字符串
发布于 2009-10-29 19:25:35
out.write("document.write(\"< a href='\" + str + \"'> \" + str.slice(beg + 1, end) + \" </a>\");");发布于 2009-10-29 19:20:33
您不能关闭<a>标记!
document.write("<a href='" + str + "'>" + str.slice(beg+1, end) + "</a>");发布于 2009-10-29 21:44:26
out.write("document.write('<a href='str'> '+str.slice(beg+1,end)+' </a>');");哇,你这里有四种级别的字符串编码, - 难怪会让你迷惑。在HTML节点中的JavaScript string文字中有一个文本字符串,在HTML <script>块中的Java string文字中有一个文本字符串。
最好避免做这样的事情,因为它很容易出错。如果str包含<或&,那么问题是由于嵌入在HTML中(可能会导致跨站点脚本安全漏洞);如果str包含空格或引号,则问题是由于嵌入了属性值;</序列是<script>块中的无效HTML...
虽然你可以通过修改自己的字符串转义函数来解决这个问题,但我想说的是,你最好使用不涉及将字符串绑定在一起的函数:
out.write(
    "var link= document.createElement('a');\n"+
    "link.href= str;\n"+
    "link.appendChild(document.createTextNode(str.slice(beg+1, end)));\n"+
    "document.getElementById('foo').appendChild(link);\n"
);其中,foo是希望链接出现的元素。
https://stackoverflow.com/questions/1642995
复制相似问题