gpt4 book ai didi

jquery - 源/背景网址/?

转载 作者:行者123 更新时间:2023-11-28 12:25:20 26 4
gpt4 key购买 nike

因为我必须创建一个可以更改图像 (src/background-url) 的函数。我想知道如何识别标签以及它是使用 src 还是 url 来访问图像。
让我们说

<div class="parent">
<input type="button" class="change" style="background-image:url('image1.jpg')" />
<img class="change" src="/image2.gif" />
</div>

早些时候我写了一个可以更改背景图像 url 的 fn,但是这个 fn 无法替换 < img>来源

$(function(){
$('.change').click(function(){
var url = getUrl_by_Ajax() /* this fn returns the file path */
$(this).css('background',url); /* it can only change the background image not img src*/
/* following line can change the src if this function knows already whether it's
src/url */
$(this).attr('src', url);
});
});

我的问题是:

  1. 如何动态获取选择器的标签及其图像的 css
  2. 有多少种访问图像的方式,例如按钮使用 url & img使用 src。

    <input type="button" background-img: url('...'); /><br/>
    <img src= "...." />

    ? ? ?

最佳答案

尝试这样的事情:

$(function(){
$('.change').click(function(){
var url = getUrl_by_Ajax() /* this fn returns the file path */
if ($(this).is('img'))
$(this).attr('src', url);
else
$(this).css('background', url);
});
});

关于jquery - 源/背景网址/?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7383973/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com