提取网页javascript中的字段值

Extract field value present in javascript of a webpage

本文关键字:字段 网页 javascript 提取      更新时间:2023-09-26

这是html网页中的脚本。

jQuery(function($) {
new Shopify.OptionSelectors('productSelect', {
  product: {
    "id":626976579,
    "title":"changedMacbook Air",
    "handle":"macbook-air",
    "description":"'u003cp'u003elightweight 'u003c'/p'u003e'n'u003cp'u003eawesome performance'u003c'/p'u003e'n'u003cp'u003ewow display'u003c'/p'u003e'nHello World626976579['"78000.00'"] ['"78000.00'"]''n['"78000.00'"] ['"78000.00'"]'u003cbr'u003e['"78000.00'"]'u003cbr'u003e626976579'u003cbr'u003e626976579'u003cbr'u003e626976579",
    "published_at":"2015-05-25T02:39:00-04:00",
    "created_at":"2015-05-25T02:40:44-04:00",
    "vendor":"Test_Store",
    "type":"Computers",
    "tags":[],
    "price":7800000,
    "price_min":7800000,
    "price_max":7800000,
    "available":true,
    "price_varies":false,
    "compare_at_price":null,
    "compare_at_price_min":0,
    "compare_at_price_max":0,
    "compare_at_price_varies":false,
    "variants":[{"id":1754837635,"title":"Default Title","options":["Default Title"],"option1":"Default Title","option2":null,"option3":null,"price":7800000,"weight":800,"compare_at_price":null,"inventory_quantity":-29,"inventory_management":null,"inventory_policy":"deny","available":true,"sku":"20","requires_shipping":true,"taxable":true,"barcode":"","featured_image":null}],"images":["'/'/cdn.shopify.com'/s'/files'/1'/0876'/1234'/products'/overview_wireless_hero_enhanced.png?v=1432536113"],"featured_image":"'/'/cdn.shopify.com'/s'/files'/1'/0876'/1234'/products'/overview_wireless_hero_enhanced.png?v=1432536113","options":["Title"],"content":"'u003cp'u003elightweight 'u003c'/p'u003e'n'u003cp'u003eawesome performance'u003c'/p'u003e'n'u003cp'u003ewow display'u003c'/p'u003e'nHello World626976579['"78000.00'"] ['"78000.00'"]''n['"78000.00'"] ['"78000.00'"]'u003cbr'u003e['"78000.00'"]'u003cbr'u003e626976579'u003cbr'u003e626976579'u003cbr'u003e626976579"},
    onVariantSelected: selectCallback,
    enableHistoryState: true
});

如何通过我自己的JavaScript访问"title"字段的值,这里是"changedMacbookAir"?

提前谢谢。

我不知道它是否有效,但尝试

var myProduct = new Shopify.OptionSelectors('productSelect', {
....
})

然后尝试

console.log(myProduct)

或者你可以试试这个:

$(document).ready(function(e){
    console.log(document.title);
})

我认为您可能必须通过回调传递它。

$('#productSelect').on('change', function(e) { 
    var t = e.target || e.srcElement,
        title = t.title.value;
            selectCallback(title);
            break;
        }
    }
 });

//If you want to trigger it right away for some reason, just use this...
$("#productSelect").change();

在产品模板或代码段、资产或模板中查找option_selection.js的代码。请参阅此fiddle以获取代码外观的示例。option_selection.js

你可能还想查看这个链接,它正在为产品变体设置一个onchange事件。我不确定你的最终目标是什么,但我看到你正在处理产品选项和变体,所以这可能会有所帮助。

如果需要,您可以修改option_selection.js,但很可能您只需要在document.ready.中进行一些jquery

下面是option_selection.js中的一个示例,它构建了每个选择器的名称。虽然您可能不需要对此进行修改,但请参阅上面的链接。

Shopify.OptionSelectors.prototype.buildSelectors = function() {
    for (var t = 0; t < this.product.optionNames().length; t++) {
        var e = new Shopify.SingleOptionSelector(this, t, this.product.optionNames()[t], this.product.optionValues(t));
        e.element.disabled = !1, this.selectors.push(e)
    }
    var o = this.selectorDivClass,
        i = this.product.optionNames(),
        r = Shopify.map(this.selectors, function(t) {
            var e = document.createElement("div");
            if (e.setAttribute("class", o), i.length > 1) {
                var r = document.createElement("label");
                r.htmlFor = t.element.id, r.innerHTML = t.name, e.appendChild(r)
            }
            return e.appendChild(t.element), e
        });
    return r
},