【问题标题】:How to remove this section from being enclosed in <li> tags如何从 <li> 标记中删除此部分
【发布时间】:2016-01-18 13:49:49
【问题描述】:

我找不到此页面的“实际”后端 (http://www.drink-driving-lawyers.com.au/pleading-guilty-or-not-guilty),我可以在其中删除使第一段带有项目符号的 &lt;li&gt;&lt;ul&gt; 标记。

认罪或不认罪的决定是您在酒后驾车案件中做出的最重要的决定。如果为酒后驾驶指控辩护,请务必从您的律师那里获得所有信息。在页面下方,有许多针对酒后驾驶指控的辩护理由。

我们将 Wordpress 用作 CMS,每当我通过 WP Admin 转到该页面的后端时,该部分没有项目符号,也没有包含在 &lt;ul&gt;&lt;li&gt; 代码中。然而,当您检查来源时,它显示为:

<ul class="postul">
<li class="first-child last-child">
<p>The decision to plead guilty or not guilty is the most important decision that you make in a drink driving case. It is very important if defending a drink driving charge to have all the information from your lawyer. Further down the page there are a number of defences to drink driving charges.</p>
<p>You have to weigh up the chance of winning a defence of your drink driving charges with the extra cost and the possibly worse penalty you may receive.</p>

我需要调整什么文件来删除这些&lt;ul&gt;&lt;li&gt; 标签?

【问题讨论】:

  • 你不介意用jQuery来解决这个问题吗?或者您会坚持使用服务器端解决方案?
  • 我很想学习两种方式

标签: html wordpress content-management-system


【解决方案1】:

客户端

您可以轻松地将这个位包含在您的Appearance &gt; Editor &gt; footer.php(或 header.php)中,并通过摆脱 &lt;li class="first-child last-child"&gt; 来解决问题:

<script type="text/javascript">
$(document).ready(function(){
var myList = $(".postul > li > *").first(); //find the first element after li
$(myList).unwrap(); //remove what's containing it without removing itself
});
</script>

阅读 cmets,一定很容易理解我想要做什么。但是让我们逐行进行:

$(document).ready(function()

等到 DOM 为 ready

var myList = $(".postul > li > *").first();

我想要一个名为myList 的变量。此变量表示(从 * 读取到开头)= whateverli 下,其中li 位于另一个具有名为postul 的类的元素中。

星号 = 任何元素(&lt;div&gt;,&lt;p&gt; 或其他任何元素!)

另外,first() 正如它所说,仅返回具有该条件的first 结果(因此页面中的其余项目不受影响)。

换句话说,它说找到这个:

<element class="postul">
    <li>
        <something> <!-- I WANT THIS! --> 
        <something> <!-- and NOT this --> 
    </li>
</element>
<element class="postul">
    <li>
        <something> <!-- NOT this either --> 
    </li>
</element>

最后:

$(myList).unwrap();

现在找到我的元素后,unwrap() 它。这意味着:

从 DOM 中移除匹配元素集的父元素, 将匹配的元素留在原处。

这将导致我们的示例代码为:

<element class="postul">
        <something> <!-- its parent is removed but it's still here --> 
        <something> <!-- this is NOT affected --> 
</element>
<element class="postul">
    <li>
        <something> <!-- NEITHER this is affected --> 
    </li>
</element>

这应该会导致您想要实现的目标。

服务器端

很难说出它的确切来源(虽然无法访问您的后端),但它确实会影响您拥有的每一篇文章,因此它来自您的服务器端。这仅取决于您的模板和帖子的结构,但您可以查看single.php 或模板中任何其他与帖子相关的 PHP 页面。

【讨论】:

  • 感谢您的反馈。我尝试了“客户端”解决方案,虽然它确实从第一段中删除了项目符号,但它也从下面的列表中删除了项目符号 - 情况不应该如此。这实际上是一开始的样子,但后来我调整了一个 CSS 文件,下半部分列表的项目符号终于出现了——但第一段中不需要的项目符号也出现了。我想它真的必须通过服务器端来解决。将继续查看文件。
  • 哎呀,已编辑!请检查并让我知道。它现在应该可以工作了:)
  • 嗯...编辑后的脚本实际上适用于指示的页面。我把它放在一个普通文件的footer.php 文件中。但是,当我检查其他页面时,它并没有解决此页面上的相同问题:drink-driving-lawyers.com.au/…。不知道为什么考虑到 footer.php 文件也服务于这个其他页面。所以我想我真的需要通过客户端来做。
  • 啊,我明白了。这是因为在您的其他页面中,&lt;div&gt; 紧跟在 &lt;li&gt; 之后,而不是 &lt;p&gt;。再次编辑我的代码,请检查并告诉我。
  • 谢谢你!它终于奏效了!...我知道这太过分了,你可能不会回答。但是你能像向新手解释一样详细说明代码吗?特别是这个:var myList = $(".postul &gt; li &gt; *").first();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 2022-10-31
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
相关资源
最近更新 更多