2017-06-19 54 views
0

我有一個CMS需要修改。我希望外部鏈接,在新窗口中target="_blank"PHP的幫助。我需要一個鏈接才能在新窗口中打開

這裏打開的代碼:

<?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}" ?> 

所有環節目前在靶坯開放。我怎樣才能使目標空白的外部鏈接打開?

回答

0

您需要關閉雙引號。看看你呈現的HTML &你會看到這個問題。

+3

顯示他需要做什麼,不要將它作爲練習。 – Barmar

0
<?php echo ($menu->type == 'External') ? 
    "{$menu->link} target='_blank'" : 
    "/Index/Content/Id/{$menu->id}"; 
. '"' ?> 

這有靠近你與<a href="打開了雙引號,並把目標投向引號的作用。這應該可以解決你的問題。

1

而是在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標籤,你回聲的網址。

+0

所有鏈接打開目標空白。我怎樣才能使目標空白中打開的外部鏈接? – chonko

+0

如果所有鏈接都在新窗口中打開,那麼我懷疑'$ menu-> type'沒有正確設置。 @Alon提供的代碼是正確的。 –

相關問題