简体   繁体   English

如何<a href>在InnerHTML中</a>隐藏<a href>标签</a>

[英]How to hide an <a href> tag in InnerHTML

I have an image that has an InnerHTML for "a href" tag define in the .cs file as shown below. 我有一个图像,该图像在.cs文件中定义了一个用于“ a href”标签的InnerHTML,如下所示。

_divToolTipContainer.InnerHtml = "<a href=\"javascript:__doPostBack('" + btnItemThumbnail.ClientID.Replace("_", "$") + "','')\">" +
                                           itemimage;

Now what I want is to have this "a href" tag "hide" or "set visible to false" if the textbox is empty otherwise have this click-able. 现在,我想要的是如果文本框为空,则将这个“ a href”标签“隐藏”或“设置为false设置为可见”,否则将其设置为可点击。

My code is shown below, but its not working. 我的代码如下所示,但无法正常工作。

    var oDivA1 = document.getElementById(oElementId);

    if(val === undefined || val == null || val.length <= 0){        
        oDivA1.style.display = "none"; // not working
    }else{
        oDivA1.style.display = "block"; // not working
    }

oDivA1.style.display = "none", this display none for the entire div not the "a href" tag only. oDivA1.style.display =“ none”,整个​​div都不显示,而不仅仅是“ a href”标记。

Is that possible? 那可能吗?

Thanks 谢谢

RJ RJ

Use the firstChild property to get the inner element of the div, assuming that's all the HTML it contains as shown in your example. 使用firstChild属性获取div的内部元素,假定它是示例中所示的所有HTML。

var oDivA1 = document.getElementById(oElementId);
var anchor = oDivA1 ? oDivA1.firstChild : null;

if(anchor && (val === undefined || val == null || val.length <= 0)){        
    anchor.style.display = "none";
}else{
    anchor.style.display = "block";
}

To prevent the link action 防止链接动作

var handler = function(e) { e.preventDefault(); };

if(anchor && (val === undefined || val == null || val.length <= 0)){        
    anchor.addEventListener('click',handler,false);
}else{
    anchor.removeEventListener('click',handler,false);
}

See https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener for information on browser compatibility and workarounds for older browsers. 请参阅https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener以获取有关浏览器兼容性和较旧浏览器的解决方法的信息。

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

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