开发者

jQuery check and get attribute of a child element

开发者 https://www.devze.com 2023-02-10 08:21 出处:网络
If a user places HTML in a textarea field, how can I check if text inserted is an <object> tag?

If a user places HTML in a textarea field, how can I check if text inserted is an <object> tag?

If true, I want to assign the src attribute of the <object> to a variable.

What the most efficient way to do this?

<div id="embed">
    <textarea>
        <object src="..."></object开发者_StackOverflow中文版>
    </textarea>
</div>


Try this...

var textarea = $('#embed textarea'),
    textareaValue = textarea.val(),
    textareaValueDom = $(textareaValue);

if (textareaValueDom.is('object')) {
    var src = textareaValueDom.attr('src');
    console.log(src); // http://example.com
}

jsFiddle.


var $obj = $($("#embed textarea").text());
var src = $obj.is("object") ? $obj.attr("src") : null;

fixed it

0

精彩评论

暂无评论...
验证码 换一张
取 消