English 中文(简体)
如果`如果'语句符合,如何将图像附加到表格中?
原标题:How to append an image into a table if the `if` statement matches?

我不知道是否可行,但下面我有一个简单的html表格:

<table id="plus" align="center">
  <tr>
    <th>
    </th>
  </tr>
</table>

如果 if 语句匹配, 那么能否在上面 lt;th & gt; 标签中添加图像? 如果语句不匹配, 那么我能否在 lt;th & gt; 标签中追加图像链接?

下面是 if 语句, 它实际上是jQuery, 包含一些php, 试图查找文本问题 :

if (qnum == <?php echo (int)$_SESSION[ textQuestion ]; ?>) {
  <img src="Images/plussigndisabled.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" name="plusbuttonrow"/>
} else {
  <a onclick="return plusbutton();">
  <img src="Images/plussign.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" name="plusbuttonrow"/>
  </a>
}
最佳回答

既然你说你正在使用jQuery, 你可以做到这一点

....
    var enabledImage = "plussign.jpg";
    var disabledImage = "plussigndisabled.jpg";
    var selectedImage;
    var imageHtml;

    if(...) {
        selectedImage = enabledImage;
    } else {
        selectedImage = disabledImage;
    }  

    imageHtml = getImageHtml(selectedImage);
    $( #ElementID ).append($(imageHtml));
....

function getImageHtml(imageFileName) {
    return  <img src="Images/  + imageFileName +  " width="30" height="30" alt="Look Up Previous Question" class="plusimage" name="plusbuttonrow"/> ;
}
问题回答

对TH应用以下独有的代号:

<table id="plus" align="center">
  <tr>
    <th id="foo">
    </th>
  </tr>
</table>

然后使用 HTML 内部属性。

var bar=document.getElementByID( foo );
bar.innerHtml="<img />";

(这假设正本是响应用户输入运行的, 即页面完成装入后)

无论是哪种方式,你都要附加某种东西, 所以确定 < em> what 您要用 < code> if- statement 重新附加, 然后追加。 无论哪种方式, 它都在同一地方 。

$("#plus th").html(function(){
    return ( qnum === 5 )
        ? "<img />"
        : "<a><img /></a>" ;
});

在此点, 附加 < code> data 。

<强力工作演示 http://jsfidd.net/26Jaf/

良好API:http://api.jquery.com/ append/" rel="nofolp">http://api.jquery.com/ppend/

<强度 > 代码

var qnum = 1;  
var append_data = "";
if (qnum == 1) { // i.e. if condition satisfy.

   append_data =  <img src="http://images.wikia.com/maditsmadfunny/images/5/54/Hulk-from-the-movie.jpg" width="30" height="30" alt="Look Up Previous Question" class="plusimage" name="plusbuttonrow"/> ;


} else {
   //do something else

alert( d );
}
$("#plus tr th").append(append_data);
​




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签