Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

javascript - Magnific popup: Get current element in callback

In Magnific Popup, I want to get an attribute in the link that is clicked and use it in a callback function (using callbacks: open) to make some changes in the DOM.

How can I do this? For example, in the code below, it should return 'it works' to console. Instead it prints 'doesnt work'. Please help!!

<a href="#test-popup" class="open-popup-link" myatt="hello">Show inline popup</a>

<script src="jquery.magnetic.custom.js"></script>

<script>

    $(document).ready(function() {
      $('.open-popup-link').magnificPopup({
        type:'inline',
        midClick: true,
        callbacks: {
          open: function() {

            if ($(this).attr('myatt')=="hello") 
            { 
              // do something 
              console.log("it works");
            }
            else
            {
              console.log("doesnt work");
            }

          },
          close: function() {

          }
        }

      });
    });

</script>

<div id="test-popup" class="white-popup mfp-hide">
  Popup content
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For Magnific Popup v0.9.8

var magnificPopup = $.magnificPopup.instance,
              cur = magnificPopup.st.el;
console.log(cur.attr('myatt'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...