最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to select elements inside data-title with jQuery? - Stack Overflow

programmeradmin3浏览0评论

I have html form like this: How can I select Gray / Silver from data-title?

 <div class="value">
   <div class="color-box grey-silver-color-gradient" data-title="Grey / Silver" data-toggle="tooltip" data-original-title="" title=""></div>
</div>

I have wrote my code but not get the result. here

var data = $('.value').data('title');
console.log(data);

Pls help me. Thanks in advace

I have html form like this: How can I select Gray / Silver from data-title?

 <div class="value">
   <div class="color-box grey-silver-color-gradient" data-title="Grey / Silver" data-toggle="tooltip" data-original-title="" title=""></div>
</div>

I have wrote my code but not get the result. here

var data = $('.value').data('title');
console.log(data);

Pls help me. Thanks in advace

Share Improve this question asked Dec 16, 2015 at 8:28 Doo DooDoo Doo 1,3472 gold badges11 silver badges28 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 5

Your problem is the data-title is on the child element to .value.

try:-

var data = $('.value > .color-box').data('title');
console.log(data);

or

var data = $('.color-box').data('title');
console.log(data);

You should do

var data = $(".color-box.grey-silver-color-gradient").data("title");
console.log(data);

You can get attribute from tag by using attr from jquery

Example:

$('*[userattribute]').attr('userattribute');

You can select it by jquery method like this

$(selector).data('name');

in your case like this

var data = $('.value > .color-box').data('name');

You are selecting one element and then expecting to get a property of another element. $('.value') selects the parent div of the one you want to select.

You should try this, keeping specific to a child of $('.value'):

var data = $('.value .color-box.grey-silver-color-gradient').data("title");
console.log(data);

or any child where data-title attribute has a value:

var data = $(".value [data-title!='']").data("title");
console.log(data);

jsfiddle: https://jsfiddle.net/o3ffptj3/1/

You can use attr()

var data = $('.value div').attr('data-title');
发布评论

评论列表(0)

  1. 暂无评论