1

In a div with id #prlogo i have am image. I need to copy the filename or location of the image on the server to a text filed with id #input_2_16, when i hover over a button with id #button.

Sounds simple but i've been pulling my hair out trying to do this...

Div html:

<div id="prlogo" class="prlogo"><img class="logoplace" src="../preview/logo-place.png"/>
        </div>

Miro

4

3 に答える 3

0

より多くのコンテキストなしで、jQueryを使用します。

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() { //this anonymous function will run when the page is ready
    $("#button").hover(function() {
      //mouse enter
      var imgSrc = $("#prlogo img").attr("src"); 
      //assumes there is an <img /> tag as a child of the #prlogo div
      $("#input_2_16").val(imgSrc);
    },function() {
      //mouse leave
    });
});
</script>

マウスのままで何もしたくない場合は、代わりに行うことができます

$("#button").mouseenter(function() {
  //mouse enter
  var imgSrc = $("#prlogo img").attr("src"); 
  //assumes there is an <img /> tag as a child of the #prlogo div
  $("#input_2_16").val(imgSrc);
});
于 2012-04-04T14:56:41.317 に答える
0

これを正しく読むと、次のようになります。

$( function () {
    $( '#button' ).mouseover( function () {
        var src = $( '#prlogo img' ).attr( 'src' );
        $( '#input_2_16' ).val( src );
    } );
} );
​
于 2012-04-04T14:57:50.947 に答える
0

DOMを複数レベル下に移動する必要がある場合は、.childrenの代わりに.findを使用してください。

$('#button').on('hover', function(){
        $('#input_2_16').val($('#prlogo').children('img').attr('src'));
});
于 2012-04-04T15:11:34.993 に答える