根据 LI ID 删除整个菜单 LI 项目

发布于 2025-01-08 14:11:53 字数 1281 浏览 0 评论 0原文

我正在使用以下代码块:

<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>

我已经遇到了很多不同的模式,但我似乎无法让任何模式满足我的需要。我一直在使用 /^(?:

  • .*?
  • \s*)/ 但我知道它还很遥远。基本上我需要使用正则表达式根据 Id 查找和删除 LI,这将被动态处理。所以如果上面是一个菜单,我需要删除 5 个。如果我能让正则表达式突出显示 5 例如 http://regexpal.com/ 我应该能够总结一下。

    更新: 我需要使用非基于 js 的函数来完成此操作,因此不需要 jquery。具体来说,我正在使用: http://cfquickdocs.com/cf9/#rematchnocase

    I am working with the following block of code:

    <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>
    

    I have com across quite a few different patterns, but I can not seem to get any to work for what I need. I have been working with /^(?:<li>.*?</li>\s*)/ but I know it's way off. Basically I need to use regex to find and remove an LI based on the Id, which will be handled dynamically. so if the above were a menu, I would need to remove 5 for example. If I can get the regex working to highlight 5 for example in http://regexpal.com/ I should be able to wrap this up.

    Update:
    I need to use a non js based function to accomplish this, so no jquery. specifically I am using the : http://cfquickdocs.com/cf9/#rematchnocase

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(7

    通知家属抬走 2025-01-15 14:11:53

    这里真正的答案是你不应该使用正则表达式来解析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。

    The real answer here is that you should not be using a regular expression to parse HTML, because HTML is not a regular language.

    Instead, use the Java library JSOUP from within your CF code to manipulate the HTML you're working with.

    After you download the jar and add it to your CF classpath, you can do things like this:

    <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>
    

    I've tested this, and it outputs exactly what you're asking for - the original HTML without the specified LI element.

    绮烟 2025-01-15 14:11:53

    不确定 Coldfusion 的特定语法,但如果它支持非贪婪量词,那么这将起作用。

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

    Not sure about coldfusion specific syntax, but if it supports non-greedy quantifiers then this will work.

    /<li[^>]*id="id5"[\s\S]*?<\/li>/i
    
    幽蝶幻影 2025-01-15 14:11:53

    您已经用 jQuery 标记了您的问题,所以:

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

    会这样做。或者,如果 id 位于变量中:

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

    您无需担心按元素类型进行选择,因为您正在分配唯一的 id,对吧?

    或者删除第 _n_th li 元素:

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

    You've tagged your question with jQuery, so:

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

    Will do it. Or if the id is in a variable:

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

    You don't need to worry about selecting by element type because you are assigning unique ids right?

    Or to remove the _n_th li element:

    $("li").eq(4).remove(); // remove fifth element (zero-based indexes)
    
    甜尕妞 2025-01-15 14:11:53

    您似乎用太多语言标记了您的问题,没有意义,但这是 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();
    ?>
    

    You appear to have tagged your question with way too many languages to make sense, but here's the answer for 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();
    ?>
    
    拥抱影子 2025-01-15 14:11:53

    我认为Kolink提供的表达方式最适合你。假设您将 HTML 存储在名为“htmllist”的变量中,则此代码应该可以工作:

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

    正如 Jake 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)>
    

    这并不能更好地解决原始问题,但如果问题变得更加复杂,可能会有所帮助。

    I think the expression provided by Kolink will serve you best. Assuming you have your HTML stored in a variable called "htmllist", this code should work:

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

    As Jake Feasel mentioned, HTML is not regular, but this should work. If your problem gets more complicated (for example if you need to take action on how different attributes relate) an XML-based solution may serve you well.

    Assuming that your "htmllist" variable includes the surrounding "UL" tags and is valid XML, the following code would be equivalent to the regular expression solution posted above.

    <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)>
    

    This is no better to solve the original problem, but might help as a starting point if it gets more complicated.

    清晰传感 2025-01-15 14:11:53

    我会这样做...它不使用 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>
    

    I would do it this way... It doesn't use rematchnocase, but rather uses ColdFusion XML utilities, an XMLSearch/XPath query, and returns the expected results. You'll need to add the root element though (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>
    
    仙女山的月亮 2025-01-15 14:11:53

    感谢我的队友@Alex Brown 提供了更新的正则表达式函数,感谢@Paul Alexander 提供了有关 php/cf 版本的正确方向,感谢@Splash-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#">
    

    Thank you to my teammate @Alex Brown for the updated regex functions, @Paul Alexander for the correct direction on the php/cf versions and @Splash-x @nnnnn for the jquery versions I needed. here is a test case for the cf side, will update once I get the jquery setup:

    <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#">
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文