我有一个需要修改的CMS。我希望在新窗口中打开外部链接target="_blank"
代码如下:
<?php foreach ($this->menus as $menu) { ?>
<a href="<?php echo $menu->type == 'External' ? $menu->link : "/Index/Content/Id/{$menu->id}" ?>">
我尝试过的:
<?php echo ($menu->type == 'External') ? "{$menu->link} target=_blank" : "/Index/Content/Id/{$menu->id}" ?>
当前在目标空白中打开的所有链接。如何使外部链接仅打开目标空白?
不要在
src
属性中进行操作,而是使代码更具可读性:
<?php if( $menu->type == 'External' ) { ?>
<a href="<?php echo $menu->link; ?>" target="_blank">
<?php } else { ?>
<a href="/Index/Content/Id/<?php echo $menu->id; ?>">
<?php } ?>
目前,此行:
<?php echo ($menu->type == 'External') ? "{$menu->link} target=_blank" : "/Index/Content/Id/{$menu->id}" ?>
将创建以下格式的链接:
<a href="http://example.com target=_blank">
将其更改为
<?php echo ($menu->type == 'External') ? "{$menu->link}" target="_blank" : "/Index/Content/Id/{$menu->id}" ?>
将修复它,您可以使用自己的方式来做到这一点,因为您用双引号 ( "
( 关闭 href
属性,然后才在回显三元运算符的结果时添加 target
属性 - 您需要考虑您正在包装的 php 标签"
你回显 url 的地方。
您需要关闭双引号。 看看你渲染的HTML,你会看到问题所在。
<?php echo ($menu->type == 'External') ?
"{$menu->link} target='_blank'" :
"/Index/Content/Id/{$menu->id}";
. '"' ?>
这具有关闭用<a href="
打开的双引号并将目标放入引号的效果。 这应该可以解决您的问题。