代码之家  ›  专栏  ›  技术社区  ›  Kanika Rathore

访问使用Iframe呈现的html中的文本区域,Iframe本身由cshtml页面加载

  •  0
  • Kanika Rathore  · 技术社区  · 9 年前

    我在Cshtml页面中加载了一个iframe。Iframe包含一个html表单。加载主页(cshtml页面)时,我需要在表单的每个文本区域前面添加一个图像。我尝试了很多事情,但都没有成功。下面是我正在尝试的代码:

    $("#iframe").ready(function() {
        $.each($('input[type=text],textarea'), function() {
    
            var idOfCurrentElement =  $(this).attr('id');
            var classNameOfCurrentElement =  $(this).attr('class');
            var visibilityOfCurrentElement = $(this).css('visibility'); 
    
            if(classNameOfCurrentElement != "hidden" && visibilityOfCurrentElement == "visible")        
                $("<img src='~/images/pic' alt='Pic'>");
        });
    });
    
    1 回复  |  直到 9 年前
        1
  •  0
  •   Mehmet Otkun    9 年前

    您必须访问iframe内容,而不是您的页面。

    $("#iframe").ready(function() {
          $.each($('#iframe').contents().find('input[type=text],textarea'), function() {
             var $thatButton =  $(this);
             var idOfCurrentElement = thatButton .attr('id');
             var classNameOfCurrentElement =  thatButton ).attr('class');
             var visibilityOfCurrentElement = thatButton .css('visibility'); 
    
         if(classNameOfCurrentElement != "hidden" && visibilityOfCurrentElement == "visible")        
             //do what you want.
          });
    });