开发者

How add another class to a javascript function

开发者 https://www.devze.com 2023-03-21 07:08 出处:网络
here\'s my js: function DoNav(theUrl) { document.location.href = theUrl; } here\'s my alternating color table code

here's my js:

function DoNav(theUrl) {
  document.location.href = theUrl;
}

here's my alternating color table code

while($i < $num) {
  if ($i % 2 == 开发者_JS百科0) {
    echo "<tr class='even' onclick=\"DoNav('physicianInfo.php');\">";
  }
  else {
    echo "<tr class='odd' onclick=\"DoNav('physicianInfo.php');\">";
  }
}

but I wanted to add a class="colorbox" when I click the row

class colorbox allows me to have an iframe modal: http://colorpowered.com/colorbox/

since my TR has a class for alternating colors, and I have a clickable row with javascript to open a link physicianInfo.php. How can I call colorbox (class="colorbox") inside the tr. can I have 2 class in 1?

I usually call the class via "a href"

Sample: <a class='colorbox' href="physicianInfo.php">


You have to initialize the plugin properly, since you are adding the html dynamically after page load.

while($i < $num) {
    if ($i % 2 == 0) {
        echo "<tr class='even tr-colorbox' \">";
    } else {
        echo "<tr class='odd tr-colorbox' \">";
    }
}

$('.tr-colorbox').colorbox({href: 'physicianInfo.php', iframe: true});

Another option

function DoNav(theUrl) {
    $.colorbox({href: theUrl, iframe: true});
}

while($i < $num) {
    if ($i % 2 == 0) {
        echo "<tr class='even' onclick=\"DoNav('physicianInfo.php');\">";
    } else {
        echo "<tr class='odd' onclick=\"DoNav('physicianInfo.php');\">";
    }
}


.addClass('colorbox')

would probably be the answer you want :)


Probable solution:

  1. in the doNav function get classes for the row
  2. then append colorbox class with those.
  3. then use javascript setAttribute method

The javascript function should be as follows

 function doNav(row,theUrl){
 // document.location.href = theUrl;
 var classes = row.getAttribute("class");
 classes+=" colorbox";
 row.setAttribute("class",classes); 
}

The full solution could be found here. I did this in jsfiddle for you.

But I am afraid that colorbox plugin will not react if you add classname (.colorbox) on the fly. It expect the class on document load.

0

精彩评论

暂无评论...
验证码 换一张
取 消