jQuery获取嵌入式小部件自动完成文本的价值

时间:2018-10-03 15:11:06

标签: javascript jquery html google-tag-manager

我已经将一个小部件嵌入到第三方站点提供的我的站点中,该小部件显示一个输入框,该输入框会在输入时自动向用户建议文本。

this page上的Ask Your Question搜索相同。

我通过在文档widget.js中包含一个<head>文件并通过html调用它来嵌入窗口小部件。

<div id="widget-5032"></div>

当我检查小部件的HTML时,我看到以下内容(示例项目列表);

<div>
    <ul id="search-query-5032_list">
        <li data-question="Alaska">Alaska</li>
        <li data-question="Hawaii">Hawaii</li>
        <li data-question="etc.">etc.</li>
    </ul>
</div>

正在从第三方网站提供自动建议,当单击一个时,将在输入中填充建议的文本,并且页面会自动重定向到外部网站(无需单击search按钮)。我无法控制小部件的功能。

我正在尝试捕获所选文本的值,但是我不确定该怎么做?

我要这样做的原因是,我可以使用dataLayer.push({})

将其发送到Google跟踪代码管理器

我已经尝试过了,但是没用;

$('#search-query-5032_list li').on('click', function() {
    alert($(this).text());
});

任何建议都值得赞赏。

2 个答案:

答案 0 :(得分:1)

我快速浏览了页面上的代码,我认为您可以在页面上使用这段代码。尝试/捕获很重要,因为仅当选择一个项目时才会创建隐藏的输入。

springSpace.la.widget_5032_inst.search.autocomplete.input.parentNode.addEventListener('click', function(){

try {
    alert(searchform_5032.form.querySelector('input[name=faqid]').value);
}
catch(err) {
    console.log("empty");
}

});

在我收到“警告”消息的地方,您将用GTM dataLayer推送代替,以将该信息导入GTM / GA。

答案 1 :(得分:-1)

您能尝试一下吗?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        let cell = Placestableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath);

        //first text
        let attributestitle = [NSAttributedStringKey.font:
            UIFont(name: "Helvetica-Bold", size: 15.0)!,
                          NSAttributedStringKey.foregroundColor: UIColor.black] as [NSAttributedStringKey: Any]

        //second text
        let attributedString = NSMutableAttributedString(string: "\t"+String(places[indexPath.row].distance!)+" miles", attributes: [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 8.0)!,NSAttributedStringKey.foregroundColor: UIColor.black])


        let myParagraphStyle = NSMutableParagraphStyle()
        myParagraphStyle.alignment = .right
        myParagraphStyle.tabStops = [
            NSTextTab(textAlignment: .right, location: 300, options: [:]),
        ]

        let attributedStringtitle  = NSMutableAttributedString(string: places[indexPath.row].title!, attributes: attributestitle)
        //adding the right alignment to the second text alone
        attributedString.addAttributes([.paragraphStyle: myParagraphStyle], range: NSRange(location: 0, length: attributedString.length))

        //combining two texts with different alignments.
        let combination = NSMutableAttributedString()
        combination.append(attributedStringtitle)
        combination.append(attributedString)
        cell.textLabel?.attributedText = combination
        return cell;
    }
$('#search-query-5032_list').on('click', 'li', function(){
    alert($(this).text());
});