javascript - 使用Jquery显示图像 "find"

标签 javascript jquery html

我正在尝试使用 jquery 显示图像。我有一个以 ID 和 PATH 作为参数的函数。ID 表示部分(每个部分都是一个在用户操作时加载的 html 页面),还有一个我在其中显示文本文件内容的文本区域。现在我想以同样的方式显示图像,我尝试了不同的方法,但没有成功。下面是我的 Java 脚本文件和 HTML 文件

HTML 文件:

<html>
    <body>
        <div  id="tab1"  name="tab" style="cursor: pointer; cursor: hand; width:90px;float: left;height: 22px ; margin-top: 0px;
        background-color:lightgrey;font-size: 13px;text-align: center;border-bottom: none;display: table-cell;"                 onClick="tabs(1)">
        <div style="margin-top: 3px" >TAB1</div>
        </div>
        <div   id="tab2" name="tab" style="cursor: pointer; cursor: hand; width:90px;background-color:lightgrey;float: left;height: 20px; margin-top: 2px; font-size: 12px;text-align: center" onClick="tabs(2)">
        <div style="margin-top: 3px">TAB2</div>
        </div>
        <div style="width:auto;height: 22px; border-bottom-color: white;border-bottom-style: solid;border-bottom-width: 1px"></div>  



        <div name="tab_content"   style="display: block; margin-left:20px;" >
            <br></br>   
            <br></br>   
            <div id="legendReport">
            Notes 1:
            </div>
            <textarea   name="textArea1" rows="4" value="" cols="50"></textarea> 
            <br></br>   
            <br></br>   
            <div id="legendRough">
            Notes 2:
            </div>
            <textarea id="roughNotes_textArea" name="textArea2" rows="4" cols="50"></textarea>    
        </div> 

        <div name="tab_content"  class="tab_content" style="display: none;margin-left:20px;">
            <br></br>   
            <br></br>                    
            <textarea  name="textArea3" rows="4" cols="50" ></textarea>  
            <br></br>   
            <br></br>   
            <textarea  name="textArea4" rows="4" cols="50"></textarea>  
            <br></br>   
            <br></br>
            <br></br>   
            <br></br>                           
            <!-- Schematic of selected analysis Point -->
            <img id="myImage" src="" style="width:304px;height:228px;"></img>   
        </div>

    </body>
</html>   

JavaScript 文件:

//Displays the file content in text area
function displayTextfileData(textFilePath,ID){
    readTextFile(textFilePath);
    alert(textFileContent);

    var sectionDIV = "#section" + ID;
    alert(sectionDIV)

    $(sectionDIV).find("[name=textArea3]").val(textFileContent);
    $(sectionDIV).find("[name=textArea4]").val(textFileContent);
}

//This function display the image
function displayImage(imageFilePath,ID){
    var sectionDIV = "#section" + ID;
    alert("displayImageforDropdown():"+imageFilePath);
    //$(sectionDIV).find("myImage").src = imageFilePath;
    $(sectionDIV).getElementById("myImage").src = imageFilePath;
    //document.getElementById("myImage").src = imageFilePath
    //alert("displayImageforDropdown(): Assigned result: "+(document.getElementByName("displayImage").src = imageFilePath));
}

显示文本文件内容工作正常,但我希望在图像上也有相同的内容

最佳答案

替换

$(sectionDIV).getElementById("myImage").src = imageFilePath;

$(sectionDIV).find("img#myImage").attr("src", imageFilePath);;

关于javascript - 使用Jquery显示图像 "find",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36351860/

相关文章:

javascript - jsplumb 连接不工作

javascript - 在外部标签上追加/前置空格

javascript - 将值设置为嵌套对象在 Angular 中不起作用

javascript - 扩展 jQuery 的 $.post() 函数

javascript - 将 json 列表动态绑定(bind)到 HTML

javascript - 在内容页面上触发 .Net 中的 OnLoad Javascript

php - 插入数据库时​​需要识别动态输入/文本区域

javascript - 如何在 Javascript 变量中添加单引号?

javascript - 如何使用 jquery 或 javascript 更改图像的透明度

javascript - 如果电子邮件输入处于焦点状态,则阻止输入按钮表单提交(jquery)