简体   繁体   English

如何将数据库中的值附加到定位标记的href属性?

[英]How can I append a value from the database to an anchor tag's href attribute?

I am using a popup jquery that pops up a static block of code using a href=#?w=300?h=200 but I need apart from the size to pass a variable from a sql database also a href=?prop_id=$id . 我正在使用弹出式jQuery,它使用href =#?w = 300?h = 200弹出一个静态代码块,但我需要除了大小之外还要从sql数据库传递变量,同时还要href =?prop_id = $ ID 。 I have tried to combine both of them but nothing seems to work does anybody has a clue?? 我试图将两者结合起来,但是似乎没有任何工作有任何线索吗?

thank you 谢谢

$(document).ready(function() {



//When you click on a link with class of poplight and the href starts with a # 
$('a.poplight[href^=#]').click(function() {
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="to_use/close.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(function() {
        $('#fade, a.close').remove();  //fade them both out
    });
    return false;
});

});

A simpler approach would be to use data- attributes in combination with jQuery's .data() method 一种更简单的方法是将data-属性与jQuery的.data()方法结合使用

HTML 的HTML

 <a href="#" class="poplight" data-propId="123" data-w="300" data-h="200">Text</a>

JS JS

   /* in click handler */
  var data=$(this).data();
  var propId=data.propId;/* same as $(this).data('propId') */
  var popWidth=data.w;
   /* etc..*/

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

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