简体   繁体   English

使用Jquery检查自动生成的span类标记

[英]check auto generated span class tag with Jquery

i am trying to check an auto generated span class="" content , which i will use it as a condition to determine whether i will show a message to the user or not 我正在尝试检查自动生成的span class=""内容,我将使用它作为条件来确定我是否会向用户显示消息

here is the span 这是跨度

<span id="sprytextfield1" class="textfieldInvalidFormatState">

what i tried here is to put a Div called email with an id so i can use the following 我在这里尝试的是将一个名为emailDiv放入id以便我可以使用以下内容

if ($('email').html('<span id="sprytextfield1" class="textfieldInvalidFormatState">'))
{

alert ("error");
}

but this did not work because if put the span inside my div email this will stop the JavaScript and result in error, because for some data condition i can not close the div at the end of the span. 但是这没有用,因为如果将span放在我的div email这将停止JavaScript并导致错误,因为对于某些数据条件,我无法在跨度结束时关闭div。

is there any way to get the span class without putting all the span inside a div ? 有什么方法可以获得span class而不将所有跨度放在div中?

Check: http://jsfiddle.net/pratik136/YY5RR/ 检查: http//jsfiddle.net/pratik136/YY5RR/

This will let you get the class of all spans. 这将让你获得所有跨度的类。

If you want to append a span to your div and still get a reference to it (so you can modify it further), I'd suggest doing like this: 如果你想为你的div附加一个跨度并且仍然得到它的引用(所以你可以进一步修改它),我建议这样做:

$("#email").html(""); // optional: clear email
var mySpan = $('<span id="sprytextfield1" class="textfieldInvalidFormatState"/>').appendTo($('#email'));

Then you can add more things to the span, like text or other elements: 然后,您可以在跨度中添加更多内容,例如文本或其他元素:

mySpan.text("some text");
mySpan.append('<a>some link</a>');

You mentioned you can't close the div after the span, I understood that you want to add more elements to it, is that correct? 你提到你不能在跨度后关闭div,我明白你要为它添加更多元素,这是正确的吗? Just append (or prepend) them, after creating the span: 在创建跨度后,只需追加(或预先添加)它们:

$("#email").append(someElement); // Will appear in the end, after the span
$("#email").prepend(someElement); // Will appear in the beginning, before the span

Update: re-reading your question, it's not clear to me what you're trying to check. 更新:重新阅读你的问题,我不清楚你要检查什么。 Will the span be already in your document, and you just want to see whether or not it has a specific class (that you already know)? 跨度是否已经在您的文档中,您只想查看它是否具有特定类(您已经知道)? If that's the case, get the element (you have its id) and use hasClass on it: 如果是这种情况,请获取元素(您有其id)并在其上使用hasClass

if ( $("#sprytextfield1").hasClass("textfieldInvalidFormatState") ) {
    alert("error");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM