2011-03-07 54 views
8

更新:
答案#3結束了最佳工作。我很可能做了其他建議有問題的地方; #3可能是最容易實現的。如果你很好奇,我試過的例子的解決方案可以在這裏找到(現在):將<span>的文本轉換爲超鏈接

  1. http://dl.dropbox.com/u/1007716/spanToUrl/test01.html
  2. HTTP: //dl.dropbox.com/u/1007716/spanToUrl/test02.html
  3. http://dl.dropbox.com/u/1007716/spanToUrl/test03.html(冠軍)
  4. http://dl.dropbox.com/u/1007716/spanToUrl/test04.html
  5. HTTP ://dl.dropbox.com/u/1007716/spanToUrl/test05.html
  6. http://dl.dropbox.com/u/1007716/spanToUrl/test06.html

原貼:
我有一個<span>標籤內的一個純文本的網站地址。我想這<跨度>標籤變成正確的超級鏈接與target="_blank"

我已經把我與這裏的工作一個詳細的例子:http://bit.ly/spantourl

如果你不想點擊進入,這裏是我有:

<span>http://www.domain.com/about</span> 

,我需要更改爲:

<a href="http://www.domain.com/about" target="_blank">http://www.domain.com/about</a> 
+0

能你添加一個類到輸出範圍? – Josh 2011-03-07 02:56:05

+0

不,但我可以將輸出範圍用一個類包裝到另一個範圍,以便它可以是 http://www.domain.com/about ox4dboy 2011-03-07 02:58:48

+0

不需要classes/id - 在特定元素 – 2011-03-07 03:07:50

回答

15

試試這個:

$('.sampleClass span').replaceWith(function() { 
    var url = $.trim($(this).text()); 
    return '<a href="' + url + '" target="_blank">' + url + '</a>'; 
}); 

不需要each,因爲replaceWith可以迭代多個元素並可以將函數作爲參數。

看看您的示例HTML,我發現它只是第一個包含URL的<td>。如果確實只有一個,你可以添加first()到選擇鏈,就像這樣:

$('.sampleClass span').first().replaceWith(/* ... */); 

如果是關係到整個包含鏈接,比你想每隔匹配操作。通過附加:even你的選擇,這樣做:

$('.sampleClass span:even').first().replaceWith(/* ... */); 

(是的,:even,而不是:odd選擇第1,第3,&Ç元素,因爲從零開始的索引。)

+0

我喜歡從其他「競爭」答案中學習。我說,快活好吧。 – 2011-03-07 03:09:10

+0

嘿,謝謝你的加油。我同意:看到多個並行的實現是這個網站的最好的事情之一。 – Ori 2011-03-07 03:17:38

+0

也在這一個,在這裏: 一個鏡頭https://dl.dropbox.com/u/1007716/spanToUrl/test03.html 沒有運氣得到這個工作。 – ox4dboy 2011-03-07 03:39:39

2

認沽ID和像

var linkText = $("#yourspanid").text(); 

$("<a/>").attr({"href": linkText, "target": "_blank"}).text(linkText).appendTo("body"); 
$("#yourspanid").remove(); 

那麼你可以做一些改變,根據您的編輯跨度

var elems = $("span.myClass > span"); 
elems.each(function(){ 
    var linkText= $(this).text(); 
    $("<a/>").attr({"href": linkText, "target": "_blank"}).text(linkText).appendTo("body"); 
}); 
elems.remove(); 

看到一個working demo

+0

上使用for-each循環,可能像所有其他元素一樣頂起這個元素,但這不是工作形式,請參閱: https://dl.dropbox.com/u/1007716/ spanToUrl/test05.html – ox4dboy 2011-03-07 03:52:04

+0

我該怎麼做。 – s84 2011-03-07 03:55:02

2

你需要有某種形式的身份,以做從節點A到節點B的轉換。我會建議沿着以下幾行:

JQuery代碼:

<script type="text/javascript"> 

    $('.convertableIdentifier').each(function(i, el) { 

     // grab the url and the link text 
     var url = $(el).html(); 

     // create a new node with query by decorating a 
     // empty a tag 
     var newNode = $('<a></a>').attr('href', url).attr('target', '_blank').html(url); 

     // replace the current node with our new node 
     $(el).replaceWith(newNode); 

    }); 

</script> 

的HTML:

<span class="convertableIdentifier">http://www.google.com</span> 
<span class="convertableIdentifier">http://www.youtube.com</span> 
<span class="convertableIdentifier">http://www.facebook.com</span> 

上面的代碼沒有進行測試,但應該有希望導致你在正確的方向。

+0

'.replaceWith()'可以遍歷一個集合,所以不需要將它包裝在'each'中。看到我的答案。 – Ori 2011-03-07 03:06:41

+0

做了一個jsfiddle它http://jsfiddle.net/GyK3q/ – Loktar 2011-03-07 03:06:48

+0

拍了一下這裏: https://dl.dropbox.com/u/1007716/spanToUrl/test01.html 沒有運氣得到它的工作在我的網頁上,即使它在jsfiddle工作,在這裏:http://jsfiddle.net/GyK3q/ – ox4dboy 2011-03-07 03:37:17

2

使用jQuery。

給跨度的ID:

<span id="linkChange">http://domain.com</span> 

jQuery代碼:

var href = jQuery('#linkChange').html(); 
var link = "<a href='"+href+"' target='_blank'>"+href+"</a>"; 

jQuery('#linkChange').replaceWith(link); 
+0

沒有運氣,嘗試此解決方案在這裏: https://dl.dropbox。com/u/1007716/spanToUrl/test04.html – ox4dboy 2011-03-07 03:42:07

+0

爲我工作... – Gautam 2017-07-05 18:35:18

1

也許是這樣的:(不需要知道IDS /班)for-each循環期運用jquerys和specificly目標跨越TDS內

$(文件)。就緒(函數(){ $( 'TD跨度' ).each(function(){ $(this).text(「」+ $(this).text()+「」); }); });

編輯:此代碼的效果要好得多:

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $('td span').each(function(){ 
      $(this).html("<a href='" + $(this).html() + "' />" + 
       $(this).html() + "</a>"); 
     }); 
    }); 
</script> 

的原始使用jQuery的哪個HTML逃脫<>字符.text()功能,無意中插件&GT; &LT;到DOM,而實際上的.html輸出正確的標籤

+0

哇,愛這個社區。新手如何選擇這麼多潛在的答案?謝謝你的jsfiddle鏈接 - 忘記了那個網站。我不確定哪個答案最好,我會在CMS中測試一些,看看它是否有效。 – ox4dboy 2011-03-07 03:23:45

+0

我雙重嵌套和修改腳本以反映雙跨度,它接近工作,但輸出不太正確。單引號似乎需要雙引號才能起作用。看到這裏: http://dl.dropbox.com/u/1007716/spanToUrl/test02.html – ox4dboy 2011-03-07 03:49:02

+0

很好,很高興看到它接近爲你工作!無論何時你確實得到它的工作,不要忘記標記爲答案哪個回答最有幫助! – 2011-03-07 05:41:58

相關問題