如何附加内联样式以添加一行文字

时间:2016-12-23 17:22:55

标签: jquery

我有很多带有“鸡”类的div,这些都有一个独特的内嵌背景图像样式,我希望能够在.jpg之前的BG图像上添加“@ 2x”结束 - 所以一个例子是 - div看起来像这样:

<div class="chicken" style="background-image: url('example.jpg');">

我希望能够添加“@ 2x”使其成为:

<div class="chicken" style="background-image: url('example@2x.jpg');">

注意:我有很多div,所以图片名称总是不同......

e.g。

<div class="chicken" style="background-image: url('example@2x.jpg');">
<div class="chicken" style="background-image: url('monkey@2x.jpg');">
<div class="chicken" style="background-image: url('nana@2x.jpg');">
<div class="chicken" style="background-image: url('tin@2x.jpg');">
<div class="chicken" style="background-image: url('crisps@2x.jpg');">

因此需要一种方法在每个div上的“.jpg”之前添加“@ 2x” 有什么想法吗?

1 个答案:

答案 0 :(得分:4)

您可以使用dataSource

执行此操作

import Foundation
import UIKit

struct DisplayableData {
    let title: String
    let description: String
    // etc...
}

class DataViewController: UIViewController {
    var data: DisplayableData?
    // You can add data as an initalizer parameter, depending on your design needs
    init() {
        // If you would create a xib for your DataViewController, than replace nib name with DataViewController to make it work
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class PageViewController: UIPageViewController {

    lazy internal var pages: [DataViewController] = {
        // If the UI is the same, reuse the viewController, no need to create multiple ones
        // You could create a xib, and draw the UI there 
        let firstVC = DataViewController()
        // Assign your data structure to your viewController
        firstVC.data = DisplayableData(title: "first", description: "desc")

        let secondVC = DataViewController()
        secondVC.data = DisplayableData(title: "second", description: "desc")

        let thirdVC = DataViewController()
        thirdVC.data = DisplayableData(title: "third", description: "desc")

        return [firstVC, secondVC, thirdVC]
    }()


    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        // Set your viewControllers
        setViewControllers([pages.first!], direction: .forward, animated: false, completion: nil)
    }
}

extension PageViewController: UIPageViewControllerDataSource {

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {

        // Lets check if the viewController is the right type
        guard let viewController = viewController as? DataViewController else {
            fatalError("Invalid viewController type in PageViewController")
        }

         // Load the next one, if it is the last, load the first one
        let presentedVCIndex: Int! = pages.index(of: viewController)
        if presentedVCIndex + 1 > pages.count - 1 {
            return pages.first
        }
        return pages[presentedVCIndex + 1]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

        // Lets check if the viewController is the right type
        guard let viewController = viewController as? DataViewController else {
            fatalError("Invalid viewController type in PageViewController")
        }

        // Load the previous one, if it is the first, load the last one
        let presentedVCIndex: Int! = pages.index(of: viewController)
        if presentedVCIndex - 1 < 0 {
            return pages.last
        }

        return pages[presentedVCIndex - 1]

    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
        return pages.count
    }
}
.each()

这实际上有效:

preview

enter image description here