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

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

问题描述

我无法删除ID为带句点或星号的div.

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?

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

谢谢 吉恩

推荐答案

替代方法

我认为您可以完全不弄乱任何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>

可以在跨度点击处理程序中检索,例如:

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

或使用\\转义字符*..这是一个jQuery风格的解决方案

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

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

为我在所有主流浏览器上均可工作. 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中的值必须唯一,并且必须至少包含一个字符.值不得包含任何空格字符.

如果该值不是空字符串,则用户代理必须将该元素与给定值(确切地说,包括任何空格字符)相关联,以用于元素的主子树中的ID匹配(例如,用于CSS中的选择器或用于 getElementById()在DOM中的方法).

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为&amp;“.xxx&amp;"的div或span的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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