【发布时间】:2018-08-11 06:00:52
【问题描述】:
我正在尝试根据以下代码在本地主机的 wordpress 中实现照片滑块:
https://github.com/devfreelex/sliderdevmean 和视频 https://www.youtube.com/watch?v=_nSPiKWWmhs
问题如下:
在索引页面中我有 html 代码:
<article class="slider_item active" slider-bg="image/slider/slide1.jpg">
<div class="slider_content">
<h1>The title</h1>
<p>Lorem ipsum dolor sit amet, nemo voluptatum accusamus!</p>
</div>
在 jQuery 页面中我有 js 代码:
// DEFINE BACKGROUND DO SLIDE
(function($){
$('.slider_item').each(function(){
var sliderBg = $(this).attr('slider-bg');
$(this).css({'background-image': 'url("http://localhost/wordpress/wp-content/themes/photographysite/"'+sliderBg+')'});
});
}(jQuery));
目标是将url传递给CSS
element.style { background-image: url('http://localhost/wordpress/wp-content/themes/photographysite/image/slider/slide1.jpg');
}
http://localhost/wordpress/wp-content/themes/photographysite/image/slider/slide1.jpg 不起作用,那么如何将 url 参数与图像一起传递?
【问题讨论】:
-
/photographysite/"'+去掉 URL 中的双引号。应该移到最后')'追加 -
我不知道sliderBg 是什么。如果它与问题中的一样,那么它是一个不完整的网址。您实际上只需要将
"移动到字符串连接中的正确位置。'url("http://localhost/wordpress/wp-content/themes/photographysite/'+sliderBg+'")' -
嗨 Taplar 你的意思是:
然后是 $(this).css({'background-image': 'url ('+sliderBg+')'});也没有用 -
如果您从同一个地方获取所有图像,则将变量设置为该目的地可能会更容易。
-
@João 不,我是说如果您更改连接问题,那么您无需更改有关元素上的值或其他任何内容的任何其他内容。现在您正在错误地构建 url,因为它将被构建为
'url("http://localhost/wordpress/wp-content/themes/photographysite/"image/slider/slide1.jpg)'。你明白为什么这种连接是错误的了吗?
标签: jquery css wordpress url slider