javascript - <td> 内的动态 anchor 标记

标签 javascript jquery html asp.net-mvc-4 razor

我有一个table对于 <td> 之一我想生成<a>使用 JavaScript 动态标记.

编写一小段代码。

  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody>
      @foreach (var item in list)
      {
         <tr>
            <td>...</td>
            <td>...</td>
            <td>  MyFunction(item.Parameter1)   </td> // dynamic anchor tag
         </tr>
      }
    </tbody>
  </table>

JavaScript 函数。

MyFunction(Parameter1)
{
    var aTag = "";
    .....
    .....   //some formatting as per needs

    aTag = '<a id="' + someId + '" alt="' + someAlt + '" title="' + someTitle + '" style ="color:Blue;text-decoration:underline;" href="#" onclick="fnAnotherFunction(' + P1 + ',' + P2 + ');">' + NameofTheTag + '</a>';

    return aTag;
}

返回aTag的字符串如何形成<a>标签位于<tr> <td> ..?

这可能吗,或者他们是否有更好的解决方案。

最佳答案

您可以使用.append().appendTo()

$('selectorForTD).append(MyFunction(item.Parameter1))`

var anch = MyFunction(item.Parameter1);
anch.appendTo($('selectorForTD');

认为这就是你所追求的。

编辑: 由于您使用的是 jQuery,因此您可以创建 anchor 元素,例如:

MyFunction(Parameter1)
{
    // var aTag = "";
    .....
    .....   //some formatting as per needs

    var aTag = $("<a>",{id:someID, alt:someAlt, title:someTitle}).css({ ...cssRules...}).click(function(e)
    {
        fnAnotherFunction(P1,P2);
    });

    return aTag;
}

这是一个quick fiddle来说明。

关于javascript - <td> 内的动态 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106691/

相关文章:

php - IE8 : JavaScript is null or not an object error?

javascript - 我无法使用 jquery 进行提交

javascript - 在 PHP 或 javascript 中将 ö é ü 等字符转换为 HTML 代码

应用于正文的 HTML5 全屏 API

html - Bootstrap设计容器-不同颜色的流体

javascript - react : How to open a newly added accordion item?

php - 使用 PHP 和 javaScript 的登录解决方案的密码散列

javascript - 如何克服图像悬停中的这个问题?

php - 尝试使用用户名和密码创建用户登录

javascript - 将文本附加到 Twitter 共享链接