如何使用javaScript在运行时设置HTML属性值?
我想做一个主题切换 我的想法是创建一个包含许多锚元素的div类,这些锚元素包含一个包含不同样本a、b、c、d、e的数据主题属性。我想用用户单击链接时选择的数据主题替换每个页面的数据主题属性。我如何使用JavaScript实现这一点 我建议使用。然后,您可以执行以下操作:如何使用javaScript在运行时设置HTML属性值?,javascript,jquery,jquery-mobile,Javascript,Jquery,Jquery Mobile,我想做一个主题切换 我的想法是创建一个包含许多锚元素的div类,这些锚元素包含一个包含不同样本a、b、c、d、e的数据主题属性。我想用用户单击链接时选择的数据主题替换每个页面的数据主题属性。我如何使用JavaScript实现这一点 我建议使用。然后,您可以执行以下操作: $('#selector').prop('data-theme') = 'newValue'; 选择器允许您针对一个或多个不同的元素。我建议使用。然后,您可以执行以下操作: $('#selector').prop('data-
$('#selector').prop('data-theme') = 'newValue';
选择器允许您针对一个或多个不同的元素。我建议使用。然后,您可以执行以下操作:
$('#selector').prop('data-theme') = 'newValue';
选择器允许您针对一个或多个不同的元素。有几种方法。我将使用jQuery来完成这项工作,因为我很懒:
var divThatCanHaveItsAttributesChanged = $("#divThatCanHaveItsAttributesChanged");
$("a.linkThatCanBeClickedToChangeTheme").on('click', function () {
var linkThatWasClicked = $(this);
divThatCanHaveItsAttributesChanged.attr('data-theme', linkThatWasClicked.attr('data-theme'));
});
请原谅超长的变量名,但希望它比a/b/c更清晰
编辑:
好的,在我们的谈话之后,您想要的是以下内容:
var divThatCanHaveItsAttributesChanged = $("#divThatCanHaveItsAttributesChanged");
$("a.linkThatCanBeClickedToChangeTheme").on('click', function () {
var linkThatWasClicked = $(this);
var theme = linkThatWasClicked.attr('data-theme');
divThatCanHaveItsAttributesChanged.attr('data-theme', theme);
eraseCookie('theme');
createCookie('theme', theme, 365);
});
抓取靠近底部的代码,并将其用作设置和读取cookie的起点。因此,现在您已经有了一个名为“theme”的cookie集,您有了一些选项。我确信首选的方法是让您使用的后端语言读取cookie并更改您使用的模板。例如,使用PHP:
$theme = empty($_COOKIE['theme']) ? 'default-theme' : $_COOKIE['theme'];
然后你可以用它把“主题”放在正确的位置。或者,如果您没有这种功能,可以使用javascript来实现
$.ready(function () {
var theme = readCookie('theme');
//Either the cookie has expired or it doesn't exist
if(!theme) {return;}
$("#divThatCanHaveItsAttributesChanged").attr('data-theme', theme);
});
不过,随着主题的改变,这可能会引起闪光。处理这个问题的方法有很多种,但至少您需要设置cookie或将结果发送到服务器以附加到此人的帐户。。取决于复杂性。有两种方法。我将使用jQuery来完成这项工作,因为我很懒:
var divThatCanHaveItsAttributesChanged = $("#divThatCanHaveItsAttributesChanged");
$("a.linkThatCanBeClickedToChangeTheme").on('click', function () {
var linkThatWasClicked = $(this);
divThatCanHaveItsAttributesChanged.attr('data-theme', linkThatWasClicked.attr('data-theme'));
});
请原谅超长的变量名,但希望它比a/b/c更清晰
编辑:
好的,在我们的谈话之后,您想要的是以下内容:
var divThatCanHaveItsAttributesChanged = $("#divThatCanHaveItsAttributesChanged");
$("a.linkThatCanBeClickedToChangeTheme").on('click', function () {
var linkThatWasClicked = $(this);
var theme = linkThatWasClicked.attr('data-theme');
divThatCanHaveItsAttributesChanged.attr('data-theme', theme);
eraseCookie('theme');
createCookie('theme', theme, 365);
});
抓取靠近底部的代码,并将其用作设置和读取cookie的起点。因此,现在您已经有了一个名为“theme”的cookie集,您有了一些选项。我确信首选的方法是让您使用的后端语言读取cookie并更改您使用的模板。例如,使用PHP:
$theme = empty($_COOKIE['theme']) ? 'default-theme' : $_COOKIE['theme'];
然后你可以用它把“主题”放在正确的位置。或者,如果您没有这种功能,可以使用javascript来实现
$.ready(function () {
var theme = readCookie('theme');
//Either the cookie has expired or it doesn't exist
if(!theme) {return;}
$("#divThatCanHaveItsAttributesChanged").attr('data-theme', theme);
});
不过,随着主题的改变,这可能会引起闪光。处理这个问题的方法有很多种,但至少您需要设置cookie或将结果发送到服务器以附加到此人的帐户。。取决于复杂性。就像我一样,格林说jquerymobile的主题滚轴工作正常 但是我认为更改是不必要的,因为您可以设置一个样例,比如a,然后在每个主题更改中,您只需替换css,所以这样就不需要修改HTML DOM
祝你好运 就像我做的一样,格林说jquerymobile的主题滚轴工作正常 但是我认为更改是不必要的,因为您可以设置一个样例,比如a,然后在每个主题更改中,您只需替换css,所以这样就不需要修改HTML DOM
祝你好运 您可以针对特定的小部件,通过更改特定于主题的类来更新其主题: //set a theme letter to change to var theme = 'a'; //update the button widgets on the current page $.mobile.activePage.find('.ui-btn').not('.ui-li-divider') .removeClass('ui-btn-up-a ui-btn-up-b ui-btn-up-c ui-btn-up-d ui-btn-up-e ui-btn-hover-a ui-btn-hover-b ui-btn-hover-c ui-btn-hover-d ui-btn-hover-e') .addClass('ui-btn-up-' + theme) .attr('data-theme', theme); //update the list-divider elements $.mobile.activePage.find('.ui-li-divider').each(function (index, obj) { if ($(this).parent().attr('data-divider-theme') == 'undefined') { $(this).removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e') .addClass('ui-bar-b') .attr('data-theme', 'b'); } }) //update the header, footer, and body $.mobile.activePage.find('.ui-header, .ui-footer') .removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e') .addClass('ui-bar-' + theme) .attr('data-theme', theme); $.mobile.activePage.removeClass('ui-body-a ui-body-b ui-body-c ui-body-d ui-body-e') .addClass('ui-body-' + theme) .attr('data-theme', theme); 如果您想将不同类型的小部件定位为不同的主题,只需使选择器更具体一点:
//give all form inputs the `a` theme
$('input').attr('data-theme', 'a');
//give all the header elements the `a` theme
$('[data-role="header"]').attr('data-theme', 'a');
您可以在用户选择样例时强制重新加载,然后将样例作为GET变量加载,并在页面加载时以及jQuery Mobile更改初始化任何内容之前读取该变量:
<script src="jQuery-Core.js"></script>
<script>
//check if there are any GET variables
if (window.location.search != '') {
var swatch = '',
arr = window.location.search.replace('?').split('&');
//loop through the GET variable key/pairs
for (var i = 0; len = arr.length; i < len; i++) {
//split into key/pair
var pair = arr[i].split('=');
//check if the key is `swatch` and if so then set the `swatch` variable to its value
if (pair[0] === 'swatch') {
swatch = pair[1];
}
}
//the `swatch` variable now holds the GET variable `swatch` if it was set
if (swatch !== '') {
$('[data-theme]').attr('data-theme', swatch);
}
}
</script>
<script src="jQuery-Mobile.js"></script>
请注意标记的顺序。您可以针对特定的小部件,通过更改特定于主题的类来更新其主题: //set a theme letter to change to var theme = 'a'; //update the button widgets on the current page $.mobile.activePage.find('.ui-btn').not('.ui-li-divider') .removeClass('ui-btn-up-a ui-btn-up-b ui-btn-up-c ui-btn-up-d ui-btn-up-e ui-btn-hover-a ui-btn-hover-b ui-btn-hover-c ui-btn-hover-d ui-btn-hover-e') .addClass('ui-btn-up-' + theme) .attr('data-theme', theme); //update the list-divider elements $.mobile.activePage.find('.ui-li-divider').each(function (index, obj) { if ($(this).parent().attr('data-divider-theme') == 'undefined') { $(this).removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e') .addClass('ui-bar-b') .attr('data-theme', 'b'); } }) //update the header, footer, and body $.mobile.activePage.find('.ui-header, .ui-footer') .removeClass('ui-bar-a ui-bar-b ui-bar-c ui-bar-d ui-bar-e') .addClass('ui-bar-' + theme) .attr('data-theme', theme); $.mobile.activePage.removeClass('ui-body-a ui-body-b ui-body-c ui-body-d ui-body-e') .addClass('ui-body-' + theme) .attr('data-theme', theme); 如果您想将不同类型的小部件定位为不同的主题,只需使选择器更具体一点:
//give all form inputs the `a` theme
$('input').attr('data-theme', 'a');
//give all the header elements the `a` theme
$('[data-role="header"]').attr('data-theme', 'a');
您可以在用户选择样例时强制重新加载,然后将样例作为GET变量加载,并在页面加载时以及jQuery Mobile更改初始化任何内容之前读取该变量:
<script src="jQuery-Core.js"></script>
<script>
//check if there are any GET variables
if (window.location.search != '') {
var swatch = '',
arr = window.location.search.replace('?').split('&');
//loop through the GET variable key/pairs
for (var i = 0; len = arr.length; i < len; i++) {
//split into key/pair
var pair = arr[i].split('=');
//check if the key is `swatch` and if so then set the `swatch` variable to its value
if (pair[0] === 'swatch') {
swatch = pair[1];
}
}
//the `swatch` variable now holds the GET variable `swatch` if it was set
if (swatch !== '') {
$('[data-theme]').attr('data-theme', swatch);
}
}
</script>
<script src="jQuery-Mobile.js"></script>
注意标签的顺序。你看过jquerymobile网站了吗?他们有这个功能。。。只要看看它们的源代码,你不能仅仅改变元素的数据主题属性,期望它们的主题会改变,你必须改变与小部件相关的基于主题的类,让主题在已经初始化的小部件上动态改变。要了解这一点,只需查看文档并检查开发人员工具中的一些代码:您看过jquerymobile站点了吗?他们有这个功能。。。只要看看它们的来源,你不能仅仅改变元素的数据主题属性,期望它们的主题会改变,你必须改变主题
-基于与小部件关联的类,以便在已初始化的小部件上动态更改主题。要了解这一点,只需查看文档并检查开发人员工具中的一些代码:它不起作用,为了更清楚,我有许多链接,每个链接都包含一个数据主题属性,这些属性将替换所有其他页面中的其他数据主题属性,即“所有其他页面”,您是说要保存所选内容,以便在站点的其他不同页面上使用吗?i、 这不是一个单页的应用程序,而是许多不同的页面?明白了。然后,您要做的是将数据存储在cookie中,并在页面加载时将其读回。如果你想要一个例子,我可以在几个小时内写出一个,我必须开始工作,做所有的事情,试试看。在不知道如何生成HTML的情况下,“解决”这个问题有点困难,但希望扩展的示例能为您提供一些可以帮助您朝正确方向发展的东西。要了解我如何生成HTML,请查看此页面的源代码,它似乎与我的HTML构造相同,但是它只将主题应用于一个页面,我想做的是将所有页面的主题应用于一个页面,thanksit不起作用,为了更清楚,我有很多链接,每个链接都包含一个数据主题属性,这些属性将用“所有其他页面”替换所有其他页面中的其他数据主题属性,您是说要保存所选内容,以便在站点的其他不同页面上使用吗?i、 这不是一个单页的应用程序,而是许多不同的页面?明白了。然后,您要做的是将数据存储在cookie中,并在页面加载时将其读回。如果你想要一个例子,我可以在几个小时内写出一个,我必须开始工作,做所有的事情,试试看。在不知道如何生成HTML的情况下,“解决”这个问题有点困难,但希望扩展的示例能为您提供一些可以帮助您朝正确方向发展的东西。要了解我如何生成HTML,请查看此页面的源代码,它似乎与我的HTML构造相同,但是它只将主题应用于一个页面,我想做的是将所有页面的主题应用于一个页面,谢谢你有正确的想法,但我想你会用这个来代替。$'选择器'.data'theme',newValue;您应该使用数据方法来更改数据值,而不是使用propmethod来更改数据值。我只是在回答了以下问题后才注意到这是一个特定于移动的问题:我自己没有做太多移动工作!你的想法是对的,但我想你会用这个来代替选择器'.data'theme',newValue;您应该使用数据方法来更改数据值,而不是使用propmethod来更改数据值。我只是在回答了以下问题后才注意到这是一个特定于移动的问题:我自己没有做太多移动工作!