【问题标题】:Opening an image on click with new tab使用新选项卡单击打开图像
【发布时间】:2021-12-29 23:11:39
【问题描述】:

在我的应用程序中,我为之前上传的附件创建了一个小预览。所以这里要打开,需要右键预览,需要选择在新标签页中打开。

除了选择之外,当用户单击图像时,我如何更改它在新选项卡上打开它?

<td>
<img alt="" width="100"  height="100" class="ml-1" src="@Url.Action("RetrieveImage", new { id = item.Id } )"
</td>

我已经尝试过了,但是在获取图像的 Item pass 中显示错误

<img alt="" width="100"  height="100" class="ml-1" src="@Url.Action("RetrieveImage", new { id = item.Id } , new { @target="_blank"})"

【问题讨论】:

    标签: c# html asp.net-mvc asp.net-mvc-4


    【解决方案1】:

    将图像作为嵌套标签添加到 a 并为其赋予 target=_blank 值,如下所示

    <a href="@Url.Action("RetrieveImage", new { id = item.Id })" target="_blank">
        <img width="220" height="250" border="0" align="center"  src="@Url.Action("RetrieveImage", new { id = item.Id })"/>
    </a>
    

    【讨论】:

    • 我试过了,它奏效了。谢谢!
    【解决方案2】:

    以上答案是正确的,但我建议添加rel="noreferrer noopener"。因为仅使用target="_blank" 存在安全问题。 设置noopener noreferrer 可防止网络钓鱼攻击。 看看这篇文章:here

    
    <a href="your-url" target="_blank" rel="noreferrer noopener">
    
            <img alt="" width="100"  height="100" class="ml-1">
    </a>
    
    

    【讨论】:

    • 感谢您的信息。我也试试这个
    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    相关资源
    最近更新 更多