为什么这个jQuery替换()不起作用

时间:2013-03-15 03:31:53

标签: php javascript jquery html

代码位于@ http://www.iluvtrees.org/,我们正在尝试删除带有...的冒号(:)

jQuery('#events-calendar-list li a').text(this.text().replace(':', ''));

我正在远程执行此操作,并且能够使用此脚本获得更简单的字符串。选择器的难度,我们的html看起来像......

...
<ul id="events-calendar-list">
<li id="events-calendar-list-630" title="
<strong>Title: </strong><b>#62 It's Little Things
</b><br />Check your fuel consumption and tire pressure to make sure your car is running as efficiently as possible.
<br /><strong>Start Time: </strong>12:00 am<br /><strong>End Time: </strong>12:00 am<br />">events=Object { mouseover=[1]}handle=function()
<a href="http://www.iluvtrees.org/">
<strong style="display: none;">Thu 3/14/2013</strong>
: //<- - - this is what we are trying to remove - - - -
<b>#62 It's Little Things</b>
<br>
</a>
...

我目前无法访问PHP文件以从中删除。

4 个答案:

答案 0 :(得分:3)

您只需删除锚点内的文本节点:

$('a').contents().filter(function(node) {
  return this.nodeType === 3; // filter text nodes
}).remove();

Demo

答案 1 :(得分:1)

如下所示。

jQuery('#events-calendar-list li a').text(jQuery('#events-calendar-list li a').text().replace(':', ''));

在许多编程语言中,this (或自我)是一个关键字,可以在实例方法中用于引用对象已调用当前正在执行的方法。

答案 2 :(得分:1)

WORKING DEMO

好的,从您给出的链接我假设所有元素都遵循相同的格式。

所以使用这个:

// .replace('</strong>: <b>','</strong><b>'); fiddle with the HTML, you can't use .text() here

jQuery('#events-calendar-list li a').html(jQuery('#events-calendar-list li a').html().replace('</strong>: <b>','</strong><b>'));

答案 3 :(得分:0)

<html>
        <head>
                <title>Test Website</title>
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
                <script>
                        $(document).ready(function() {
                                var x = $('a#test').text();
                                alert(x.replace(':', ''));
                        });
                </script>
        </head>
<body>

<ul id="events-calendar-list">
<li id="events-calendar-list-630" title="
<strong>Title: </strong><b>#62 It's Little Things
</b><br />Check your fuel consumption and tire pressure to make sure your car is running as efficiently as possible.
<br /><strong>Start Time: </strong>12:00 am<br /><strong>End Time: </strong>12:00 am<br />">events=Object { mouseover=[1]}handle=function()
<a id="test" href="http://www.iluvtrees.org/">
<strong style="display: none;">Thu 3/14/2013</strong>
: //<- - - this is what we are trying to remove - - - -
<b>#62 It's Little Things</b>
<br>
</a>


</body>
</html>