2013-10-07 46 views
-1

我無法確定在一個字符串中放置php變量的語法。代碼如下:.attr中的PHP變量字符串

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { 
     jQuery('#pinfo').attr('href','mailto:[email protected]?subject=' . get_the_title()); 
     jQuery('#pinfo').attr('class','pullup'); 
    }; 

get_the_title()沒有顯示和代碼似乎與我目前的語法突破。

+0

你應該設置變量,然後你可以在html中使用它。你可以發佈get_the_title()的代碼嗎? – bksi

+0

get_the_title()是一個標準的wordpress函數 - http://codex.wordpress.org/Function_Reference/get_the_title –

+0

我沒有看到標籤wordpress,我無法讀取頭腦對不起。 – bksi

回答

2

當您將PHP和Javascript一起使用時,PHP會在javascript執行之前執行。所以,簡單地編寫代碼,像這樣:

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { 
     jQuery('#pinfo').attr('href','mailto:[email protected]?subject=<?=get_the_title()?>'); 
     jQuery('#pinfo').attr('class','pullup'); 
    }; 

一旦瀏覽器呈現頁面時,它會使你的JavaScript代碼看起來是這樣的(這是瀏覽器需要什麼):

if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { 
     jQuery('#pinfo').attr('href','mailto:[email protected]?subject=My Page Title'); 
     jQuery('#pinfo').attr('class','pullup'); 
    }; 
+0

謝謝!你是一個明星× –

+0

單獨註釋此代碼僅適用於ID爲'#pinfo'的第一個元素 - 如果我在同一頁上有兩個元素的ID爲'#pinfo',則第二個元素將被忽略。不知道這是如何工作的.. –

+0

注意使用PHP短打開標籤,但:http://programmers.stackexchange.com/questions/151661/is-it-bad-practice-to-use -tag-in-php –