2017-08-28 50 views
1

我使用的是Wordpress的lightbox插件,「title」屬性指定了Lightbox中的說明。這就是我使用看起來像插件的例子:在標題屬性中添加按鈕鏈接 - HTML

<a href="link.to.video" class="wplightbox" title="The description video">Video</a> 

是否有可能包括一個按鈕,上面的代碼中的title屬性中的鏈接?這是可能的HTML?

+0

看一看工具提示 – Chris

+1

您可以在標題直接使用提示,但按鈕不可能 –

+1

你試試看? – j08691

回答

1

這是不可能把任何東西比title屬性的文字:

標題屬性表示元素的參考信息,如將適合的工具提示。在鏈接上,這可能是目標資源的標題或說明;在圖像上,它可能是圖像信用或圖像描述;在一段中,它可能是對案文的腳註或評註;在引文中,它可能是關於來源的進一步信息;等等。值是文本。

來源:https://www.w3.org/TR/2011/WD-html5-20110525/elements.html#the-title-attribute

你需要考慮你的WP插件的選項,以找到一種方法,你需要還是什麼,如果插件沒有提供這樣的選擇,用另一種不改變它。

後編輯: 快速搜索後,似乎沒有任何插件的WP提供了顯示一些HTML代碼作爲描述的可能性。

如果是這種情況,那麼需要一些自定義代碼。我會用這個程序:

  • 使用鏈接的標題,例如:http://example.com/page

  • 收藏夾顯示(這應該是在js插件的情況下),轉換與http開頭的任何標題://使用JS的按鈕/ jQuery的

這裏是jQuery的代碼:

/** 
 
* This code should be run after the lightbox is displayed 
 
*/ 
 

 
//get the titles from the lightbox/lightboxes - use the appropriate selector 
 
var titleTag = $('.title'); 
 

 
//go over all the titles selected and replace the URLs with <a> tags 
 
titleTag.each(function(){ 
 
    url = findRegex($(this).text()); 
 
    if (url !== false) { 
 
    $(this).html('<a href="' + url + '" class="button">Button</a>'); 
 
    } 
 
}); 
 

 
//function to find an URL in a string 
 
//returns false if no URL in the string 
 
function findRegex(str) { 
 
    var regex = /^(https?:\/\/)?([da-z.-]+).([a-z.]{2,6})([/w .-]*)*\/?$/g; 
 
    let m; 
 
    var found = false; 
 
    while ((m = regex.exec(str)) !== null) { 
 
     // This is necessary to avoid infinite loops with zero-width matches 
 
     if (m.index === regex.lastIndex) { 
 
      regex.lastIndex++; 
 
     } 
 

 
     found = m[0]; 
 
    } 
 
    
 
    return found; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="title">http://example.com/page</div> 
 

 
<div class="title">Normal title</div>

+0

你碰巧知道任何可能提供這種選項的視頻燈箱插件嗎? – Adam

+0

該插件應允許使用HTML來描述圖像。我做了一個快速搜索,沒有一個出來,所以也許這不是一個解決方案。我正在編輯我的答案,以提供替代解決方案。 –

+0

感謝您的編輯。你有一個想法,我可以如何將任何以http://開頭的標題轉換爲使用JS的按鈕?說實話,我不太熟悉JavaScript。 – Adam