-2

how to store the returned function value in 'x' ??? i need this because, i wanna use a hover function on images and respectives image's src will varies.

<script>
var x;
$(document).ready(function(){$(":image").attr("src");
alert(x);
});
</script>
</head>
<body>

<form action="">
Name: <input type="text" name="user"><br>
Password: <input type="password" name="password"><br>
Compatible: <input type="image" src="compatible_ie.gif" width="31" height="30">
</form>
4

3 回答 3

1

好的,以防万一,如果您想在悬停时更改图像,我假设,那么您需要使用以下内容:

<img src="img1.jpg" alt="Image" class="hover" />
<img src="img2.jpg" alt="Image" class="hover" />
<img src="img3.jpg" alt="Image" class="hover" />

为这些设置一个悬停图像,例如img1a.jpg, img2a.jpgimg3a.jpg您可以这样调用它们:

$(document).ready(function(){
    $(".hover").hover(function(){
        var a = $this.attr("src")
        $(this).attr("src", $(this).data("src"));
        $(this).data("src", a);
    }, function(){
        var a = $this.data("src")
        $(this).data("src", $(this).attr("src"));
        $(this).attr("src", a);
    });
});
于 2013-10-29T07:14:04.380 回答
0

Check this i hope u will understand how to do it.
Html:

<div>
    <img src="http://web.utk.edu/~ihouse/wp-content/uploads/icon-smiley.jpg" alt="Image" class="big" />
</div>

<img src="http://icons.iconseeker.com/png/fullsize/scrap/smiley-grin.png" alt="Image" class="small" />
<img src="http://www.skinbase.org/files/archive/shots/638/Az-Smiley-Face-Icon.jpg"  class="small"/>

Script:

$(document).ready(function () {
    var oldSrc = null;
    $(".small").hover( function(){
        // get the src of hovered image
        var src= $(this).attr('src');
        //get the big image src and store it in the global variable.
        oldSrc = $(".big").attr('src');
        //set the hovered image src into the big one.
        $(".big").attr('src',src )
    }, function() {
        // reset the big image
        $(".big").attr( "src" , oldSrc);
    });
 });

Check this Fiddle

于 2013-10-29T07:21:48.237 回答
0

我认为您想要 src 属性的值。只需将其存储在变量中并在任何您想要的地方使用。

<script>
var x;
$(document).ready(function(){var x=$("input:image").attr("src");
    alert(x);
});
</script>
</head>
<body>

<form action="">
  Name: <input type="text" name="user"><br>
 Password: <input type="password" name="password"><br>
Compatible: <input type="image" src="compatible_ie.gif" width="31" height="30">
</form>
</body>
于 2013-10-29T07:14:23.257 回答