带有一组值的jquery数据属性选择器

时间:2016-10-31 09:59:13

标签: jquery html object jquery-plugins selector

我的问题是如何使用jQuery data()选择具有特定数据值的对象?例如:

$('[data-infos-parent_id=0]').html('it ok');
<div class="question" data-infos='{"parent_id":0,"my_id":0, "title":""}'>

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您可以在此使用.filter().data(key)将获取与元素一起存储的任意数据,并尝试将其转换为适当的数据类型。

$('.question[data-infos]').filter(function(){
   return $(this).data('infos')["parent_id"] == 0;
}).html('it ok');

答案 1 :(得分:1)

试试这个

https://jsfiddle.net/w1paL17d/

$('.question').each(function(){
    var json = JSON.parse($(this).attr('data-infos'));
    if(json.parent_id === 0){
        $(this).html('it ok');
    }
});

答案 2 :(得分:0)

使用set.seed(123) # Calculation of distances with small samplesize is working well N <- 100 data_100 <- data.frame(x1 = rnorm(N, 5, 10), x2 = rnorm(N, 5, 10), x3 = rnorm(N, 5, 10), x4 = rnorm(N, 5, 10), x5 = rnorm(N, 5, 10)) # Matrix with all distances (no problem for the smaller samplesize) dist_100 <- as.matrix(dist(data_100)) # Find the row with the smallest distance for(i in 1:nrow(dist_100)) { dist_100[i, i] <- Inf } low_dis <- numeric() for(i in 1:nrow(dist_100)) { low_dis[i] <- as.numeric(sort(dist_100[ , i]))[1] } low_dis_ids <- list() for(i in 1:length(low_dis)) { low_dis_ids[[i]] <- as.numeric(names(dist_100[ , i][dist_100[ , i] == low_dis[i]])) } # low_dis_ids is the desired output and stores the rows with the smallest distances # The same procedure is not working for larger samplesizes N <- 100000 data_100000 <- data.frame(x1 = rnorm(N, 5, 10), x2 = rnorm(N, 5, 10), x3 = rnorm(N, 5, 10), x4 = rnorm(N, 5, 10), x5 = rnorm(N, 5, 10)) dist_100000 <- dist(data_100000) # Error: cannot allocate vector of size 37.3 Gb 功能

html()

演示:https://jsfiddle.net/4koq5uzx/