2012-02-23 66 views
0

我用下面的代碼塊時刪除整個菜單李項:根據李ID

<li class="standby" id="id4"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id5"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id6"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id7"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 

我有遇到一些好幾個不同的模式,但我似乎無法得到任何爲工作我需要的。我一直在與/^(?:<li>.*?</li>\s*)/合作,但我知道它的方式。基本上我需要使用正則表達式來查找和刪除基於Id的LI,這將被動態處理。所以如果上面是一個菜單,我需要刪除5例如。如果我可以讓正則表達式工作,例如在http://regexpal.com/中突出顯示5,我應該可以將其包裝起來。

更新: 我需要使用非js基礎的功能來完成這個,所以沒有jQuery。特別是我現在用的是:http://cfquickdocs.com/cf9/#rematchnocase

+1

根據HTML規範,以數字開頭的ID不合法。 – 2012-02-23 21:09:34

+1

我可以讓他們開始,身份證,不用擔心,這是seudo代碼:)我現在會修復它 – 2012-02-23 21:10:49

+0

爲什麼你使用正則表達式? $('#5')。remove()不起作用?或$('li [id =「5」]')。remove()? – 2012-02-23 21:10:53

回答

0

謝謝你對我的隊友@Alex布朗的更新正則表達式的功能,@保羅亞歷山大在PHP/CF版本和@飛濺-X @nnnnn爲我所需要的jQuery的版本的正確方向。這裏是一個cf側的測試用例,一旦我得到jquery設置就會更新:

<cfsavecontent variable="Psuedo"> 
<li class="standby" id="id4"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id5"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id6"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id7"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
</cfsavecontent> 
<cfscript > 
t=structNew(); 
//find one and kill it 
t.Psuedo=Psuedo; 
t.1.testCase1='<li[^>]*?id="id5".*?li>'; 
t.1.after1=reReplaceNoCase(t.Psuedo,t.1.testCase1,"","All"); 
t.1.testCase2='<li[^>]*?id="id4".*?li>'; 
t.1.after2=reReplaceNoCase(t.1.after1,t.1.testCase2,"","All"); 
t.1.testCase3='<li[^>]*?id="id7".*?li>'; 
t.1.after3=reReplaceNoCase(t.1.after2,t.1.testCase3,"","All"); 
//find more than 1 and kill them 
t.2.testCase1='<li[^>]*?id="id(5|7)".*?li>'; 
t.2.after1=reReplaceNoCase(t.Psuedo,t.2.testCase1,"","All"); 
</cfscript> 
<cfdump var="#t#"> 
0

您已經標記了使用jQuery你的問題,所以:

$("#id5").remove(); 

會做到這一點。或者,如果ID是一個變量:

var someId = "id5"; 
$("#" + someId).remove(); 

您不必擔心按元素類型選擇,因爲你權分配唯一的ID?

或刪除的_n_th li元素:

$("li").eq(4).remove(); // remove fifth element (zero-based indexes) 
+0

抱歉,我無法使用jquery – 2012-02-23 21:19:06

+0

@chrishough爲什麼你把它標記爲jquery比? – 2012-02-23 21:21:43

+0

@DamienPirsy我修正了這個線程。 – 2012-02-23 21:50:18

0

你似乎已經標記你的問題有太多的語言是有道理的,但這裏的PHP的答案:

<?php 
$dom = new DOMDocument(); 
$dom->loadXML($data); // $data is your HTML stuff 
// using loadXML so you don't get <html>, <head> and <body> tags added 
$node = $dom->getElementById('5'); 
$node->parentNode->removeChild($node); 
$result = $dom->saveXML(); 
?> 
+0

是的,我修正了,我的不好。我正在尋找正則表達式模式,我的錯誤。對不起 – 2012-02-23 21:18:55

+1

在這種情況下,[小馬他來...](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454) – 2012-02-23 21:20:33

+0

我試過這個帖子「<(?:」[^「] *」['「] * |'[^'] *'['」] * | [^'「>]] +>」get我的lis,但我需要找到一個基於傳入的值,如id5 – 2012-02-23 21:24:09

1

不肯定coldfusion特定的語法,但如果它支持非貪婪的量詞,那麼這將工作。

/<li[^>]*id="id5"[\s\S]*?<\/li>/i 

看到它http://refiddle.com/1r7

+0

嗯......你能在這裏成功測試嗎? http://regexpal.com/ – 2012-02-23 21:37:16

+0

你可以看到它的工作在http://refiddle.com/1r7 – 2012-02-23 21:38:19

+0

無視最後的評論,我現在要測試,看看它是如何工作 – 2012-02-23 21:39:35

1

這裏真正的答案是:you should not be using a regular expression to parse HTML,因爲HTML是不是一個正規的語言。

相反,使用CF庫中的Java庫JSOUP來操縱正在使用的HTML。

你下載的jar並將其添加到您的CF類路徑後,就可以做這樣的事情:

<cfset jsoup = CreateObject("java", "org.jsoup.Jsoup")> 
<cfsavecontent variable="html"> 
<li class="standby" id="id4"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id5"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id6"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
<li class="standby" id="id7"> 
<a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></LI> 
</cfsavecontent> 

<cfset htmlObj = jsoup.parse(html)> 
<cfset htmlObj.select('##id7').remove()> 

<cfoutput>#htmlObj.html()#</cfoutput> 

我測試過這一點,它輸出你問什麼了 - 原來沒有指定LI元素的HTML。

0

我認爲Kolink提供的表達方式會爲您提供最好的服務。假設你有存儲在一個名爲「htmllist」可變你的HTML,該代碼應工作:

<cfset htmllist = ReReplaceNoCase(htmllist,'<li[^>]+id="id5".*?</li>\s*','','ALL')> 

正如傑克Feasel提到的,HTML是不規律的,但這應該工作。如果您的問題變得更加複雜(例如,如果您需要針對不同屬性之間的關係採取措施),基於XML的解決方案可能會爲您提供良好的服務。

假設您的「htmllist」變量包含周圍的「UL」標記並且是有效的XML,那麼以下代碼將等同於上面發佈的正則表達式解決方案。

<cfset htmllist = ReplaceNoCase(htmllist,"LI>","li>","ALL")> 
<cfset xLis = XmlParse(htmllist,false)> 
<cfloop index="ii" from="#ArrayLen(xLis.ul.li)#" to="1" step="-1"> 
    <cfif xLis.ul.li[ii].XmlAttributes["id"] EQ "id5"> 
     <cfset ArrayDeleteAt(xLis.ul.li,ii)> 
    </cfif> 
</cfloop> 
<cfset htmllist = htmllist = ToString(xLis)> 

這對解決原始問題沒有好處,但如果它變得更加複雜,可能有助於作爲一個起點。

0

我會這樣做......它不使用rematchnocase,而是使用ColdFusion XML實用程序,XMLSearch/XPath查詢,並返回預期結果。你需要添加根元素(ul)。

<cfxml variable="list" casesensitive="false" > 
<ul> 
    <li class="standby" id="id4"> 
     <a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></li> 
    <li class="standby" id="id5"> 
     <a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></li> 
    <li class="standby" id="id6"> 
     <a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></li> 
    <li class="standby" id="id7"> 
     <a href="www.google.com" target="_self" title="Contact Information"> Contact Information<font class="menuItemType">(BB)</font></a></li> 
</ul> 
</cfxml> 
<cfset idToDelete = 'id6'> 
<cfset list = XMLSearch(list,"//li[@id!='#idToDelete#']")> 
<cfoutput>#ArrayToList(list,"")#</cfoutput> 
+0

這當然是比我的建議更直接的方法,不同之處在於你的情況,你假設有效的XHTML,而我認爲答案應該支持簡單的HTML。不過,我喜歡RegExp上的兩種方法。 – 2012-02-24 22:56:28