简体   繁体   English

如何在JQUERY中使用表达式隐藏图像控件

[英]How can hide Image control with expression in JQUERY

My question is that i have a image in a image control HTML now i need to hide that image. 我的问题是我现在在图像控件HTML中有一个图像,我需要隐藏该图像。 I have tried the code to hide the image as given below.. 我尝试了代码来隐藏图像,如下所示。

if ($("#ImageTest").attr(src="") == true) {
    $("#img").hide();
}
else {
    $("#img").show();
}

HTML code

<div id="ImageTest"> 
    <img id="img" src="C:\Image\MyImage\img.png"/> 
</div>

it is working fine when i provide an id to image control "img". 当我向图像控件“ img”提供ID时,它工作正常。 but i need the code in which i am not providing the id to the image control. 但是我需要我不向图像控件提供ID的代码。 like >given below 就像>如下所示

<div id="ImageTest"> 
    <img src="C:\Image\MyImage\img.png"/> 
</div>

I need to search the control with expression like "src" and given image path, and after that i have to hide that image control. 我需要使用“ src”之类的表达式来搜索控件,并提供给定的图像路径,然后我必须隐藏该图像控件。

You can use Attribute Equals Selector [name="value"] to select images with empty src value and hide them. 您可以使用Attribute Equals Selector [name =“ value”]选择具有空src值的图像并将其隐藏。 The images that have source not equal to empty string will be visible by default. 默认情况下将显示源不等于空字符串的图像。 You probably need to give the url instead of giving physical path of image. 您可能需要提供url而不是提供图像的物理路径。

$('img[src=""]').hide()

Your current condition also seem incorrect. 您当前的状况似乎也不正确。

Change 更改

if ($("#ImageTest").attr(src="") == true) {

To

if ($("#ImageTest").attr() == "" {

you can select using the img tag and check for src try this 您可以选择使用img标签并检查src尝试此操作

$('img').each(function(){
if ($(this).attr("src") == "") {
    $("#img").hide();
}
else {
    $("#img").show();
}
})

您可以使用以下代码:

$('img[src="C:\Image\MyImage\img.png"]').hide()

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

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