删除id =“.xxx”的div或span [英] Delete a div or span with a id=".xxx"

查看:177
本文介绍了删除id =“.xxx”的div或span的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法删除具有期间或星号的ID。

I am unable to delete a div with an id with a period or an asterix.

<div id="*xxx."></div>
<div id=".xxx*"></div>



我有jquery代码,它删除< div id = xxx>< / div> ,但不包括上述内容。不是在寻找jQuery代码,但是它需要一个正斜杠吗?

I have the jquery code, which deletes the <div id="xxx"></div> but not the above. Not looking for the jQuery code, but does it need a forward slash?

---- req by anurag --------

----req by anurag--------

// JavaScript

$('.s').click(function (e) {
    var wd_del = (e.target.id);
    var yy = wd_del.substr(1, 100);

    $(document.getElementById("" + yy)).remove();
    $(document.getElementById("s" + yy)).remove();
});

// HTML

<div id="s_t">
    <? do { $w1=$ row_ws1[ 'wd']; ?>
        <div id="<? echo $w1 ?>" class="ul_shh" style="cursor:pointer;">
            <span id="s<? echo $w1 ?>" class="s">
                x
            </span>
            <? echo $w1; ?>
        </div>
    <? } while ($row_ws1=m ysql_fetch_assoc($ws1)); ?>
</div>

感谢
Jean

Thanks Jean

推荐答案

替代方法



我认为你可以做任何id。依靠文档结构来提供信息。看起来你有一个div列表,其中每个div可以通过点击 x 删除,它是存在于div。

Alternate Approach

I think you can do without messing around with any id's at all. Rely on the document structure instead to provide the information. It looks like you have a list of divs, where each div can be deleted by clicking on an x that is present inside the div.

所以,如果你有一个结构:

So if you had a structure like:

<div class="node">
    <span class="delete"> x </span>
    ...
</div>
<div class="node">
    <span class="delete"> x </span>
    ..
</div>

将删除事件分配给所有区间,

Assign the delete event to all spans as,

$("span.delete").click(function() {
    $(this).parent('.node').remove();
});

这应该可以让你不必依赖id,只要你坚持将span放入div中并分配适当的类名的基本结构。如果您想知道被点击元素的父元素的ID,请将其存储为数据属性,而不是ID。这保持jQuery和旧的浏览器快乐。例如,

That should free you from having to rely on the id at all, as long as you stick to a basic structure of putting the span inside the div and assign appropriate class names. If you want to know the ID of the clicked element's parent, store it as a data attribute instead of an id. That keeps jQuery and older browsers happy. For example,

<div class="node" data-id="*xxx.">
    ...
</div>

可以在span click处理程序中检索,如下所示:

which can be retrieved inside the span click handler, as:

$("span.delete").click(function() {
    var node = $(this).parent('.node');
    var id = node.attr('data-id'); // do something with it
});






旧方法



使用本地 getElementById 方法查询,以确保如果用户代理认为是有效的ID,则选择该元素。 jQuery将对传入的ID做一些额外的处理,所以最好是本机查询:


Old Approach

Query using the native getElementById method to ensure that the element gets selected if the user-agent considers that to be a valid ID. jQuery will do some extra processing on the passed in ID, so it's better to query natively:

$(document.getElementById("*xxx.")).remove();
$(document.getElementById(".xxx*")).remove();

或转义字符 * c $ c>。与 \\ 。这里有一个jQuery-esque解决方案

Or escape the characters * and . with \\. Here's a jQuery-esque solution

$("#\\*xxx\\.").remove();
$("#\\.xxx\\*").remove();

适用于所有moojor浏览器。请进行IE测试自己:) 查看此示例

Works on all moojor browsers for me. Do IE tests yourself :) See this example.

请注意,根据HTML5,构成有效ID字符串的限制是:

Note, that the restrictions for what constitutes a valid ID string as per HTML5 are:


  1. 必须是唯一的

  2. 必须至少包含一个字符

  3. 不能包含任何空格字符

>从规范中引用:


id属性指定其元素的唯一标识符(ID)。该值必须在元素的主子表中的所有ID中唯一,且必须至少包含一个字符值不能包含任何空格字符

The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

如果值不是空字符串,用户代理必须将元素与给定值关联(例如对于CSS中的选择器或对于DOM中的 getElementById()方法),为了在元素的归属子树中进行ID匹配的目的(确切地说,包括任何空格字符)。

If the value is not the empty string, user agents must associate the element with the given value (exactly, including any space characters) for the purposes of ID matching within the element's home subtree (e.g. for selectors in CSS or for the getElementById() method in the DOM).

这篇关于删除id =“.xxx”的div或span的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆