简体   繁体   English

目标HTML元素(如果属性等于某个值)

[英]Target HTML element if attribute equals a certain value

I have an HTML template string that prepends some user info to a div for each user from an Ajax call. 我有一个HTML是模板字符串prepends一些用户信息的div用于从每个用户Ajax调用。 Each div has a data-stream attribute with either false or true values. 每个div都有一个data-stream属性,其falsetrue

I want to target the element using JS or jQuery and use CSS or addClass on it if the value is true. 我想使用JSjQuery定位element ,如果value true,则使用CSSaddClass

Maybe display a green icon if the value is true and a red one if false. 如果值为true,则可能显示绿色图标,如果值为false,则显示红色图标。

JS JS


var target = $('#result')

target.prepend('<div data-stream="' + userObj.stream + '" class="item "> <a target="_blank" href="' + userObj.url + '"> <img class="logo" src="' + userObj.logo + '"/> <span class="name">' + userObj.name + '</span> </a> </div>')

My attempts 我的尝试

  if ( typeof $(".item").attr('data-stream') === "true" ) {
            // style div here..
          }

that doesn't work, nor does: 那行不通,也不行:

 $('.item').filter(function(){
               var $this = $(this)
               return $this.data('data-stream') === true
            }).addClass('online')

nor: 也不:

 $("#results .item").find("[data-stream='true']")

There has got to be a pretty simple way to do this, any suggestions using jQuery or JS ? 必须有一个非常简单的方法来执行此操作,使用jQuery或JS的任何建议?

Your $("#results .item").find("[data-stream='true']") was really close , but it looks for items with that attribute as descendants of the .item elements. 您的$("#results .item").find("[data-stream='true']") 确实很接近 ,但它会查找具有该属性的项作为.item元素的后代 You want to look for .item elements that themselves have that attribute and value: 您要查找本身具有该属性和值的.item元素:

$("#results .item[data-stream='true']")

Note no space between .item and [data-stream='true'] . 注意.item[data-stream='true']之间没有空格。 It's a compound selector. 这是一个复合选择器。

Or if you have reason to do them separately as with your find , use filter rather than find (but only if you have a reason to separate the two aspects): 或者, 如果您有理由像find那样分开进行操作,请使用filter而不是find (但前提是您有理由将这两个方面分开):

// If you have reason to separate the two aspects
$("#results .item").filter("[data-stream='true']")

Live Example of the compound selector: 复合选择器的实时示例:

 var target = $('#result') target.prepend('<div data-stream="true" class="item ">This is the true one</div>'); target.prepend('<div data-stream="false" class="item ">This is the false one</div>'); console.log("Turning the true one blue with a class"); $("#result .item[data-stream='true']").addClass("foo"); 
 .foo { color: blue; } 
 <div id="result"></div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 


Side note: Part of your question uses the id result , and other parts use results . 旁注:问题的一部分使用id result ,其他部分使用results You'll want those to match. 您将希望它们匹配。

You can use only CSS, targeting data-stream attribute's value: 您只能使用CSS,定位数据流属性的值:

UPDATE: Use :before to place an icon depending on the the data attribute 更新:根据数据属性,使用:before放置图标

 .item:before { font-family: FontAwesome; font-weight: normal; font-style: normal; display: inline-block; text-decoration: inherit; margin-right:5px; } .item[data-stream="true"]:before { content: "\\f00c"; color: green; } .item[data-stream="false"]:before { content: "\\f00d"; color:red; } 
 <div data-stream="true" class="item ">True</div> <div data-stream="false" class="item ">False</div> <div data-stream="true" class="item ">True</div> <div data-stream="true" class="item ">True</div> <div data-stream="false" class="item ">False</div> <div data-stream="false" class="item ">False</div> 

When you want Javascript to Manipulate unique DOM elements you should always use IDs on those elements in order to be easily accessible. 当您希望Javascript操作唯一的DOM元素时,应始终在这些元素上使用ID,以便于访问。

Change your code to something like this : 将您的代码更改为如下所示:

target.prepend('<div id="YOUR_INIQUE_ID" data-stream="' + userObj.stream + '" class="item "> <a target="_blank" href="' + userObj.url + '"> <img class="logo" src="' + userObj.logo + '"/> <span class="name">' + userObj.name + '</span> </a> </div>')

and Access the element later using those IDs : 并稍后使用这些ID来访问元素:

jQuery('#YOUR_INIQUE_ID').css('display','none');

Addition : 加法:

Checking your question again , i noticed that you are not after Unique document elements (or want to manipulate a very specific one) but after a group of elements. 再次检查您的问题,我注意到您不是在Unique文档元素之后(或想要操纵一个非常特定的文档元素),而是在一元素之后。 My first answer is not suited for your case. 我的第一个答案不适合您的情况。

TJ Crowder proposal is suited for your case. TJ Crowder提案适合您的情况。

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

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