gpt4 book ai didi

javascript - 控制台报告奇怪的 javascript 语法错误

转载 作者:行者123 更新时间:2023-12-02 16:31:36 25 4
gpt4 key购买 nike

我使用 php 生成缩略图和标题的表格。一个简单的 JavaScript 控制这些缩略图及其标题在主图像区域中的显示。

第二行中的最后一个图像 (3-08.jpg) 给出错误“SyntaxError:参数列表后缺少 )。”

奇怪的是 ) 在那里,和其他所有的一样。而且,由于此列表是自动生成的,因此每个缩略图和标题的代码没有任何差异。

这是脚本:

function changeImage(filename, imagetitle)
{
document.getElementById('mainImage').src = filename;
document.getElementById('caption').innerHTML = imagetitle;
}

这是生成的 html:

                        <tr align="center" valign="bottom" class="thumbnails">
<td> <a href="javascript:changeImage('adminNew/images/3-01.jpg','Attractive Tamarac Trail home with comfortable living spaces and a beautifully landscaped yard.')"><img src="adminNew/images/3-01.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-02.jpg','The tiled entry is a great spot to shed those boots and coats - and a pretty spot to relax, too.')"><img src="adminNew/images/3-02.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-03.jpg','Beautiful maple flooring and exquisite detailing add a lot of character to this home.')"><img src="adminNew/images/3-03.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-04.jpg','Counter seating will allow your family to grab a quick meal while visiting with the cook.')"><img src="adminNew/images/3-04.jpg"> </a>
</td>
</tr>
<tr align="center" valign="bottom" class="thumbnails">
<td> <a href="javascript:changeImage('adminNew/images/3-05.jpg','Dining is delightful in this area full of windows.')"><img src="adminNew/images/3-05.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-06.jpg','The main floor is open and inviting.')"><img src="adminNew/images/3-06.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-07.jpg','A cozy and carpeted living room makes a wonderful gathering place for family and friends.')"><img src="adminNew/images/3-07.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-08.jpg','The lower level has a very nice family room, two bedrooms, and a full bath. But, wait... there's more!')"><img src="adminNew/images/3-08.jpg"> </a>
</td>
</tr>
<tr align="center" valign="bottom" class="thumbnails">
<td> <a href="javascript:changeImage('adminNew/images/3-09.jpg','Formerly used as a stained glass studio, the artist in your family will enjoy making this large space their own.')"><img src="adminNew/images/3-09.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-10.jpg','This spacious wood shop, complete with outside entry, will delight any woodworker.')"><img src="adminNew/images/3-10.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-11.jpg','The deck will be a popular spot during the warmer months. ')"><img src="adminNew/images/3-11.jpg"> </a>
</td>
<td> <a href="javascript:changeImage('adminNew/images/3-12.jpg','We all need a little more storage!')"><img src="adminNew/images/3-12.jpg"> </a>
</td>
</tr>

有人知道为什么会发生此错误吗?

非常感谢!

最佳答案

您的描述包含单引号并用单引号括起来,因此字符串以“wait...there”结尾,后面是(对 JS 而言)废话 s more!'

确保在构建页面之前转义描述字符串,否则您会遇到很多这样的问题。您需要(至少)转义所有引号,无论是单引号还是双引号,在它们前面加上反斜杠 (\')。您还应该以同样的方式转义反斜杠。

PHP 似乎为此提供了一个函数:addslashes

请记住,如果这些描述取自用户输入,您需要非常小心地清理和转义任何可能允许他们编写浏览器脚本的内容,否则您将很容易受到各种 XSS 的攻击。

关于javascript - 控制台报告奇怪的 javascript 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com