jQuery get path from data attribute

LAron

I have a small problem, I want to get "path" from data attribute and add to background.

HTML

<div>
  <div data-image="../images/header.jpg"></div>
</div>

jQuery

$('[data-image]').css(
    {
        background: "url("+$(this).data('image')+") no-repeat center",
        backgroundSize: "cover",
        height: ($(document).height()/ 3)
    }
);

Do you have any idea how do this?

Amit Joki

Just cache the element in a variable and use it

var elm = $('[data-image]'); // cache it
elm.css({
    background: "url("+ elm.data('image') +") no-repeat center", // use it
    backgroundSize: "cover",
    height: ($(document).height()/ 3)
});

If there are more elements, you could use each

elm.each(function(){
    $(this).css({
        background: "url("+ $(this).data('image')+ ") no-repeat center",
        backgroundSize: "cover",
        height: ($(document).height()/ 3)
    });
});

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

get property from data variable in ajax jQuery

분류에서Dev

Get Data from JSON file into jQuery

분류에서Dev

In Jquery get attribute of targeted element

분류에서Dev

jQuery $.get() and $.ajax() doesn't get data from PHP

분류에서Dev

get items from session attribute

분류에서Dev

Is it possible to get data from C#/Razor in jQuery by way of HTML?

분류에서Dev

How to get data from inputs and put them in an array using jQuery?

분류에서Dev

Using jquery in angular in returning data from $http.get request

분류에서Dev

Get data value from php array with autocomplete jquery/ajax

분류에서Dev

Jquery scroll to section based on data attribute

분류에서Dev

Get all values for a specific data attribute

분류에서Dev

Woocommerce: Get the chosen attribute from item in cart

분류에서Dev

Get a StreamSubscription from an on-* attribute in dart

분류에서Dev

get all the nested folders names (no path) from a path

분류에서Dev

Get location of "true" GVFS path from Nautilus

분류에서Dev

Laravel - Get instance of file from string path

분류에서Dev

Showing path on google map from json data

분류에서Dev

How to get the value of custom HTML attribute using JQuery?

분류에서Dev

How to pass the array value in to columns attribute of Jquery data table

분류에서Dev

Get input element inside <tr> with based on data-attribute value

분류에서Dev

Render data from/to Javascript/jQuery

분류에서Dev

자바 스크립트 / jQuery from async : false to async : true and still get data from outside

분류에서Dev

To get data from another activity

분류에서Dev

Emberjs : get data from input

분류에서Dev

How to get data from response

분류에서Dev

Unable to get data from object

분류에서Dev

How to get value of content attribute from span class

분류에서Dev

How parsing xml from url and get attribute php

분류에서Dev

jQuery.get returns old data

Related 관련 기사

  1. 1

    get property from data variable in ajax jQuery

  2. 2

    Get Data from JSON file into jQuery

  3. 3

    In Jquery get attribute of targeted element

  4. 4

    jQuery $.get() and $.ajax() doesn't get data from PHP

  5. 5

    get items from session attribute

  6. 6

    Is it possible to get data from C#/Razor in jQuery by way of HTML?

  7. 7

    How to get data from inputs and put them in an array using jQuery?

  8. 8

    Using jquery in angular in returning data from $http.get request

  9. 9

    Get data value from php array with autocomplete jquery/ajax

  10. 10

    Jquery scroll to section based on data attribute

  11. 11

    Get all values for a specific data attribute

  12. 12

    Woocommerce: Get the chosen attribute from item in cart

  13. 13

    Get a StreamSubscription from an on-* attribute in dart

  14. 14

    get all the nested folders names (no path) from a path

  15. 15

    Get location of "true" GVFS path from Nautilus

  16. 16

    Laravel - Get instance of file from string path

  17. 17

    Showing path on google map from json data

  18. 18

    How to get the value of custom HTML attribute using JQuery?

  19. 19

    How to pass the array value in to columns attribute of Jquery data table

  20. 20

    Get input element inside <tr> with based on data-attribute value

  21. 21

    Render data from/to Javascript/jQuery

  22. 22

    자바 스크립트 / jQuery from async : false to async : true and still get data from outside

  23. 23

    To get data from another activity

  24. 24

    Emberjs : get data from input

  25. 25

    How to get data from response

  26. 26

    Unable to get data from object

  27. 27

    How to get value of content attribute from span class

  28. 28

    How parsing xml from url and get attribute php

  29. 29

    jQuery.get returns old data

뜨겁다태그

보관