简体   繁体   English

从按钮属性中选择自定义标签

[英]Select the custom tag from button attribute

I have created a button element structure like below 我创建了一个如下所示的按钮元素结构

<input 
      type="button" 
      class="btn btn-primary" 
      name="redirect" 
      value="<mycustomtag data-id=15>"
      title="<mycustomtag data-id=14>"
>

Now, whenever the DOM gets ready I'm trying to find out the custom element and trying to replace with string. 现在,每当DOM准备就绪时,我都试图找出自定义元素并尝试用字符串替换。 But I'm not able to replace the custom element. 但我无法替换自定义元素。

The snippets I have used to find is as below 我过去常常找到的片段如下

jQuery("mycustomtag").each(function(){
    //process here
});

PS this works fine in the following case: PS在以下情况下工作正常:

<div><mycustomtag data-id=20></div>
<h4><mycustomtag data-id=18></h4>

your code 你的代码

jQuery("mycustomtag") jQuery的( “mycustomtag”)

will try to find tag named mycustomtag, and what i understand is you are trying to replace the input attributes right ? 将尝试找到名为mycustomtag的标签,我理解的是你正在尝试更换输入属性吗?

try following 尝试下面

 //if you want to get values var value = $("#btnCustom").attr("value"); var title = $("#btnCustom").attr("title"); alert(value); alert(title); //if you want to set values $("#btnCustom").attr("value","replacevalue"); $("#btnCustom").attr("title","replace value 2"); value = $("#btnCustom").attr("value"); title = $("#btnCustom").attr("title"); alert(value); alert(title); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" class="btn btn-primary" name="redirect" value="<mycustomtag data-id=15>" title="<mycustomtag data-id=14>" id="btnCustom" > 

You couldn't find them since the value of an attribute is considered just like a string . 您找不到它们,因为属性的值被视为字符串

To find those elements you need to select them based on the main tag by selecting the specific attribute using .prop() , like : 要找到这些元素,您需要使用.prop()选择特定属性,根据主标记选择它们,如:

$('input').each(function() {
   $(this).val();
   $(this).prop('title');
});

PS this works fine in the following case PS在以下情况下工作正常

That because in this case it's considered as a tag element in your DOM that why jQuery can find it by a simple selector. 因为在这种情况下,它被认为是DOM中的标记元素,为什么jQuery可以通过简单的选择器找到它。

 $('input').each(function() { console.log($(this).val()); console.log($(this).prop('title')); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" class="btn btn-primary" name="redirect" value="<mycustomtag data-id=15>" title="<mycustomtag data-id=14>"> 

In your first HTML code what you're looking for is in the value or title attribute. 在您的第一个HTML代码中,您要查找的是value或title属性。 In your second it's the element name. 在你的第二个它是元素名称。

To select an element based on its value, use the following syntax: 要根据其值选择元素,请使用以下语法:

$("input[value='<mycustomtag data-id=15>'")

To select an element based on its title works similarly. 根据标题选择元素的工作方式类似。

If you put your custom tag in an attribute of another tag it won't be rendered in the page, in other words it won't be part of the document DOM tree, it will be just a string in an attribute, that's why when you use jQuery("mycustomtag") you don't get anything, but it will work if you put it as a child of a div or a span . 如果将自定义标记放在另一个标记的属性中,它将不会在页面中呈现,换句话说它不会成为文档DOM树的一部分,它只是属性中的一个string ,这就是为什么当你使用jQuery("mycustomtag")你没有得到任何东西,但如果你把它作为divspan的孩子,它将会起作用。

So in your specific case you will need to use .attr() method to get it from this specific attribute or .val() method if it's in the value. 因此,在您的特定情况下,您将需要使用.attr()方法从此特定属性或.val()方法获取它,如果它在值中。

jQuery("input").attr("title");
jQuery("input").val();

Demo: 演示:

 console.log(jQuery("input").attr("title")); console.log(jQuery("input").val()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" class="btn btn-primary" name="redirect" value="<mycustomtag data-id=15>" title="<mycustomtag data-id=14>" > 

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

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